最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Handle blur event of parent element fired by focus on child element and by clicking outside of child - Stack Overfl

matteradmin3PV0评论

I have a "parent div" containing a child box input type=number. When user clicks outside of input box I use blur or focusout event of parent div to use input values at some other place.

I also need to use $('inputbox').trigger('focus') at some place, which fires "parent div"'s blur event unwantedly and runs code on that event.

Please give a solution to stop this parent blur event on child's focus OR give a way to find whether focus is made by trigger('focus') on child element or by user clicking outside of parent div.

I need to fire parent Blur only when user clicks outside of it & not when focus is triggered through code.

I have a "parent div" containing a child box input type=number. When user clicks outside of input box I use blur or focusout event of parent div to use input values at some other place.

I also need to use $('inputbox').trigger('focus') at some place, which fires "parent div"'s blur event unwantedly and runs code on that event.

Please give a solution to stop this parent blur event on child's focus OR give a way to find whether focus is made by trigger('focus') on child element or by user clicking outside of parent div.

I need to fire parent Blur only when user clicks outside of it & not when focus is triggered through code.

Share Improve this question edited Jan 19, 2016 at 9:06 Umesh K. asked Jan 19, 2016 at 6:35 Umesh K.Umesh K. 3112 gold badges6 silver badges14 bronze badges 2
  • 1 can you share a fiddle jsfiddle ? – gurvinder372 Commented Jan 19, 2016 at 6:37
  • Please see final answer. – Umesh K. Commented Jan 19, 2016 at 7:39
Add a ment  | 

4 Answers 4

Reset to default 3

This worked perfect for me:

if (e.relatedTarget === null) ...

with jquery you can make custom events very easily , something like:

$('inputbox').trigger('special-focus');

then you can wait for this event just like any other event:

$('div').on('special-focus' , function(){ ... } );

this will prevent your events from interfering with the built in ones.

I guess if you don't want to use that suggestion then do this in your click handler or your focus handler of the child

 .on('focus' , function(e){
    e.stopPropagation();
    e.preventDefault();
   /// the rest of your code ...
 });

this will stop the propagation of events to parent elements

What worked for me was checking the relatedTarget property of the eventObject object in the handler function.

$("#parentDiv").focusout(function (eventObject) {
    if (eventObject.relatedTarget.id === "childInputId")
        /// childInput is getting focus
    else
        /// childInput is not getting focus
});

.on('blur',...) of parent fires before .on('focus' ,...) of child.
Anyways for a parent div containing child input box we can use
$('input').trigger('special-focus');
and then

  $("#form" ).on('special-focus', '.parentdiv' , function(event) {
          $("#form" ).off('blur', '.parentdiv');
          $(event.target).focus();
          $("#form" ).on('blur', '.parentdiv' , showValuesOnBlur);
     });


Now blur of parent will not fire on focus of child.
This worked for me. i.e. off & on the blur event of parent inside special-focus.
Thanks Scott Selby :)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far