I have a input field. When the onchange event is triggered, I want to enable a jQuery button based on a LI element. I want to have the same behaviour as a normal button would do. The problem is, if I click on button when it’s disabled, jQuery catches the event and prevents the default behaviour to occurs, which should release the focus on the input field and trigger the onchange event before the onclick is triggered on the button.
I’m using jQuery 1.6.4 with jQuery UI 1.8.16 in Chrome.
In this live demo : http://jsfiddle.net/francisfortier/QMDc2/5/, you have three fieldsets. The first one uses a standard button, the second one, a jQuery button based on a li and the third one, use again a jQuery button based on a li with a special hack to manually release the focus from the input field on the click capture. This third fieldset can solve my problem on modern browsers, but it will not in IE8 because it requires to use event capturing.
Do you have a better cross browser solution to have the same behaviour with a jQuery button as a standard button?
Thank you
What about adding
.unbind('click')before your click handler:Demo: http://jsfiddle.net/jtbowden/yPHKp/
Of course, then you have to add the smarts to the click handler to not execute when the input is empty:
Demo: http://jsfiddle.net/jtbowden/yPHKp/1/
Or, you could add a
keyuphandler. Then the change is made before the button is clicked:Demo: http://jsfiddle.net/jtbowden/GcnYz/1/
A better solution might be to
unbindtheclick.buttonhandler and then rebind it with the addition of a.blur()event to the input fields:This retains all of your validation in place, plus forces the
changeevent to happen before the button click. You could integrate the final.click()handler into theclick.buttonevent by putting the guts in anelseblock.Demo: http://jsfiddle.net/jtbowden/GcnYz/2/