blur() vs. onblur()
I have a input tag with an onblur event listener:
<input id="myField" type="input" onblur="doSomething(this)" />
My initial thought is to call blur:
But that doesn’t work (though no error).
Why is that?
.click() will call the click event attached to an element via the onclick listener. Why does
blur() not work the same way?
works because your element (the
<input>) has an attribute called “onblur” whose value is a function. Thus, you can call it. You’re not telling the browser to simulate the actual “blur” event, however; there’s no event object created, for example.
Elements do not have a “blur” attribute (or “method” or whatever), so that’s why the first thing doesn’t work.
Contrary to what pointy says, the
blur() method does exist and is a part of the w3c standard. The following exaple will work in every modern browser (including IE):
Note that I used
link.onmouseover instead of
link.onclick, because otherwise the click itself would have removed the focus.
I guess it’s just because the onblur event is called as a result of the input losing focus, there isn’t a blur action associated with an input, like there is a click action associated with a button