user1703963
user1703963

Reputation: 25

Jquery on, focus, and blur with internet explorer

I am trying to set up my readonly text boxes as "unfocussable". Basically, I don't want to let the caret show up when the user clicks into the text area.

I was using this code:

    $(document).on("focus", ":input[readonly]", function () {
        this.blur();
      }
    );

Which works great in Fire fox, but in IE it doesn't seem to work, or at least not respond fast enough.

Thanks for any help! I am pulling my hair out

Upvotes: 0

Views: 2711

Answers (3)

user1703963
user1703963

Reputation: 25

I ended up doing this:

     $(document).on('click', function (event) {
        var doPrevent = false;
        var d = event.srcElement || event.target;
        if ((d.tagName.toUpperCase() === 'INPUT' &&  d.tagName.toUpperCase() === 'TEXTAREA') {
            doPrevent = d.readOnly || d.disabled;
        }

        if (doPrevent) {
            d.blur();
        }
    });

I think it gives IE time to work, or maybe gives it a real object. Very strange either way

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

Seems to fix your problem, force focus then blur:

http://jsfiddle.net/Y2K5a/show

$(document).on("focus", ":input[readonly]", function () {
    this.blur().focus().blur();
});

Upvotes: 1

AntouanK
AntouanK

Reputation: 4968

Should work. Tried this in IE10. http://jsfiddle.net/Lj3Te/

If not, try to set your element to readOnly like this:

document.getElementById('ID').readOnly = true;

http://msdn.microsoft.com/en-us/library/ie/ms534357(v=vs.85).aspx

Upvotes: 0

Related Questions