I have some inputs with OnBlur event handlers something like
<input name="abc" tabIndex="5" class="datetime" onblur="if (CheckMode(this))__doPostBack('abc',''); else return false;" />
In JQuery Form ready, I’d like to convert OnBlur event hanlder into something like
<input name="abc" tabIndex="5" class="datetime" onblur="....; setTimeout(function(){if (CheckMode(this))__doPostBack('abc',''); else return false;},100);" />
It means I’d like to wrap the current event handlers with some extra codes.
I’d like to call the old code with setTimeOut() after doing some extra work.
If’s possible to do on the client side like the above?
I’ve tried attr() , but it does NOT work.
The contents are changed , but nothing happens onBlur.
Also, I think , I can’t use Blur()/Live()/bind() in this situation, can I?
What you have there is called a DOM0 handler — a handler hooked up using a mechanism that isn’t defined by any DOM standard, but which is supported by all major browsers.
In your particular example, you could just attach your own
blurhandler using jQuery which attaches a modern or “DOM2” handler, which won’t displace the DOM0 handler, since all you want to do is add asetTimeoutcall. If you wanted to do anything else, you’d have to do something more complicated, because some browsers call DOM0 handlers before calling DOM2 handlers, and other browsers call DOM2 handlers before calling DOM0 handlers. But again, since what you’re doing is triggering something asynchronous (viasetTimeout), that wouldn’t matter, you don’t care which gets called first because your timeout code won’t run until later anyway.But let’s assume you want to do something more interesting:
You can replace a DOM0 handler with a modern DOM2 one. You just have to grab the DOM0 handler from the reflected property on the DOM element, attach your own handler instead, and then call the DOM0 function from your handler. You want to be sure to call the DOM0 handler in the context it expects, and with the arguments it expects. Something like this (this example uses
clickrather thanblur, but it should work the same in both cases):Live example
Note the asymmetry: We read the
onclickproperty and get a function, but we assign a string to it. Assigning a blank string toonclickis the most broadly-compatible way of clearing the handler (assigningnullorundefineddoesn’t work in some browsers). (Well, okay, the other way would betarget[0].onclick = function() { }, but why create a function when you don’t need to.)Update:
From your comment below, you’ve said you want to call the DOM0 handler after a timeout. You can do that easily enough just by wrapping the code to call it in a closure (and handling the
thisissue):And to cancel it before it happens: