<input id = "focus-ipt" type = "text" value = "nothing" />
var $focusIpt = document.querySelector( "#focus-ipt" );
$focusIpt.addEventListener( "blur", function ( e ) {
$focusIpt.value = "blured!";
} );
setTimeout( function () {
$focusIpt.focus();
}, 1000 );
I just trigger focus event,but blur event fired after focus; I searched for a long time, close the console, use setTimeout,however, can't solve it;I really don't konw how this happened,can anyone help me?
This is actually not the case;
focus()
does not automatically triggerblur()
.The
blur()
is getting triggered because you are interacting with the DOM. By clicking on one of the buttons in your fiddle (such asRun
orTidy
), the#focus-ipt
element is no longer active -- the button you're clicking on is.If you simply wait the time out, without doing anything, the
blur()
event won't occur. As soon as you click off of the input, theblur()
will occur.This can be seen in the following example:
Hope this helps! :)