2

focusout on input field will trigger every time the specific input looses its focus. But, I want to exclude some specific a tag from triggering that focusout function

Example:

<input type="text" id="name_input">
<a id="apply_name">SAVE</a>

Then the focusout function:

$("#name_input").focusout(function(e) {
  //do something here
});

Clicking on "#apply_name" also triggers focusout function of an input. How can I exclude that specific element ID from triggering it. Note: I tried some tricks already posted on StackOverflow and none of them seams to work...

Alchnemesis
  • 479
  • 8
  • 23

3 Answers3

3

Another way of doing this is checking what your target id is

var evt;
document.onmousemove = function (e) {
    e = e || window.event;
    evt = e;
}
$("#name_input").focusout(function (e) {
    if (evt.target.id == "apply_name") {
        //apply_name clicked
    } else {
        //focus out and applyname not clicked
    }
});

DEMO

Anton
  • 32,245
  • 5
  • 44
  • 54
0

You can use "blur" - event.

$("#name_input").on("blur", function(e) {
    //your code
});
P R
  • 344
  • 4
  • 18
  • blur doesn't bubble, but seems strange this could be the issue because INPUT cannot contains any element – A. Wolff Jan 13 '14 at 10:41
0

Solution from How to exclude Id from focusout Added .hasClass which I also needed:

$('#id').focusout (function (e) {

    if (e.relatedTarget && $(e.relatedTarget).hasClass('dontFocusOut')) {
        return;
    }
    //do your thing

});
Community
  • 1
  • 1
user109764
  • 576
  • 6
  • 11