IE Prevent focus (click, double click) in the selection box if it has a specific class

I use javascript to simulate a disabled select element. I can't turn it off since .net validators don't work, but that's a different story.

I have the following function:

function preventFocus(e) { if ($(this).hasClass("disabled")) { e.preventDefault(); this.blur(); } } 

It is called:

 $("#ProvinceID").toggleClass("disabled").bind('focus click dblclick', preventFocus); 

Double-clicking on the selection field in IE still allows you to display and select items. I tried the IE developer toolbar and e.type appears as focus and dblclick. Is this an IE bug, or do I need to catch some other event? I also tried to focus.

Thanks!

+4
source share
2 answers

It's been 4 years already, but I just discovered that there really is a way to get IE ≤ 8 to do what e.preventDefault() on mousedown does in other browsers (which prevents selection and ) focus): set the unselectable attribute! http://jsbin.com/yagekiji/1

Please note that unlike recommended workarounds, which always boil down to setTimeout(function() { thingIDidntWantFocusStolenFrom.focus(); }) , this prevents focus from mousedown ever stolen by the target in the first place!

What is funny about unselectable is that it is not inherited, so it is often overlooked in favor of the selectstart event (which bubbles up and e.preventDefault() , which prevents selection, but does not prevent focus) or set for each element descendant using tree traversal (e.g. fooobar.com/questions/911293 / ... fooobar.com/questions/48112 / ... answers ), but you can just set it on event.target to mousedown .

(Also, jQuery ticket: http://bugs.jquery.com/ticket/10345 )

+1
source

I do not know if this is just a typo, but you should use e.preventDefault (); it looks like you were missing in brackets.

0
source

Source: https://habr.com/ru/post/1300904/


All Articles