1

How to know where was click on blur or focusout of my input?

I've sketched a small example:

Html:

<div id ="all">
    <div id="whereIsInput">
        <input id="blur"/>
    </div>
</div>

Css:

#whereIsInput{
    height: 100px;
    width: 200px;
    background-color: yellow; 
}

JS:

function onBlur(e){
    jQuery('#all').append('<div>' + e.type + ', ' + e.target.toString() + '</div>')    
}
jQuery('#blur').blur(onBlur);
jQuery('body').on('click', onBlur);

So questions is: where was click when blur is occured?

I've investigate this: 1. can't find it in blur or focusout event arguments 2. click on body occurs later then blur

Kris Ku
  • 1,507
  • 1
  • 16
  • 32

1 Answers1

2

You can setup a flag to check if a blur event ocurred before click.

var blurred = false;//blur flag

$(document).on("click", function(){
    if(blurred){
        console.log(this);
        blurred = false;
    }
});

$("input#blur").on("blur", function(){
    blurred = true;//blur occurred
});
Wilmer
  • 2,511
  • 1
  • 14
  • 8