DA. DA. - 1 year ago 183
jQuery Question

blur() vs. onblur()

I have a input tag with an onblur event listener:

<input id="myField" type="input" onblur="doSomething(this)" />

Via JavaScript, I want to trigger the blur event on this input so that it, in turn, calls the

My initial thought is to call blur:


But that doesn't work (though no error).

This does:


Why is that?
will call the click event attached to an element via the onclick listener. Why does
not work the same way?

Answer Source



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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download