I would like to prevent the default event from a focusOut
(or blur
) event and keep the focus set to the specific input field.
This is what I have already tried:
using
event.preventDefault()
at the beginning and at the end of the function (for test purposes, not for style)return false;
to prevent propagationsetting the focus back to the element directly in the element (but I found that the focus still switches because the focus switch is executed after the
.on()
function executes. I know I could usesetTimeout
, but I’d like to avoid it and figure out the heart of the issue.)using
blur
instead offocusOut
My code is this:
_triggerEventHide: function(inst, eventType) {
inst.$target.on(eventType, function(event) {
event.preventDefault();
if (!$.suggestBox.$divCont.hasClass($.suggestBox.mouseOverClassName)) {
$.suggestBox.$divCont.css({display: 'none'}); //reposition Suggestbox
} else {
inst.target.focus();
}
event.preventDefault();
return false;
});
}
Note that $target
represents the jQuery-wrapped element and target
represents the native DOM element.
I’ve done some research and have found already a few articles relating to my question, but none of them answer this exact question.
preventDefault does not work on focus event – Here, the answer given is an alternate route to using any kind of event manipulation.
Focus back textbox on focus out if does not have required value – Here, the solution was to use
setTimeout()
to set the focus back to the original element, which is all right, but I’d like to understand the heart of the issue, whypreventDefault()
is not working here.jQuery preventDefault() not working – I tried some possible solutions from this thread, such as using
event.stopImmediatePropagation()
andevent.stop()
, but all for naught.
I appreciate any time, knowledge and solutions (including attempts) contributed in this matter. I would really like to learn…
Here is a jsFiddle to show you the problem… feel free to play around with it and try different solutions.
onblur="this.focus()"
on the unblurable element? – Gustativeelse
statement, I think theinst.target.focus();
should come before thereturn false;
– Pend