- I have an
<input type=text>
withfocusout
event handler - I have a
<button>
with click event handler
Focusout checks whether format in input box is correct. It does so by testing input value against a regular expression. If it fails it displays a message (a div
fades-in and -out after some time) and refocuses my input by calling
window.setTimout(function() { $(this).focus(); }, 10);
since I can't refocus in focusout
event handler. focusout
event can't be cancelled either. Just FYI.
Click collects data from input elements and sends it using Ajax.
The problem
When user TABs their way through the form everything is fine. When a certain input box failes formatting check it gets refocused immediately after user presses TAB.
But when user doesn't use TAB but instead clicks on each individual input field everything works fine until they click the button
. focusout
fires and sets time-out for refocusing. Since time-out is so short focusing happens afterwards and then click
event fires and issues an Ajax request.
Question
I have implemented my formatting check as an independent jQuery plugin that I want to keep that way. It uses .live()
to attach focusout
on all input fields with a particular attribute where format regular expression is defined.
Data submission is also generic and I don't want to make it dependant on formatting plugin. They should both stay independent.
How can I prevent click event from executing without making these two plugins dependant?
Example code I'm fiddling with
After some searching I've seen that all major browser support document.activeElement
but I can't make it work in Chrome. FF and IE both report this
being the active element, but Chrome always says it's BODY
that is active even though click
fired on the button
element.
Check this code http://jsfiddle.net/Anp4b/1/ and click on the button. Test with Chrome and some other browser and see the difference.
button
if there is aninput
that failed validation solve your problem? – Kiwanisclick
already fired. It would fire even thoughfocusout
would disable the button. – Oversupply