I have a webform to input a contact’s physical address that I am styling with jQuery UI. In my application code, I check to see if there is a secondary address line (like Apartment or Suite number), and show or hide a secondary address line accordingly by hiding the element. I also have a jQuery UI “Plus” Button to add an additional line on new or existing forms, which only displays one line by default. The secondary line has a Minus button exactly the same as the Plus button to remove the secondary line if necessary.
If the user clicks on the Plus button, the secondary line appears, and the Plus button is set to disabled.
The form also has a standard HTML reset button to reset the form to default values. Everything is working fine with my added javascript, except when there is a secondary address line visible (so the Plus button is set to disabled). If the reset button is clicked when the plus button is disabled, it gets set to enabled, even when there is a secondary street address line visible and it should be disabled.
I’ve set up a simple form to show this behavior here: jQuery UI Reset Button Form. By default, this form should have the Plus button disabled, and if you click on the reset button, it will become enabled as I’ve explained.
How do I prevent the HTML form from enabling the button when it shouldn’t be?
Nice challenge. Reset event ‘restores’ elements to its initial state, so your
add_address_lineanchor must start with adisabledattribute.So your initial markup shoud be this:
With this, when the form resets, your
awill return to disabled state.Hope this helps. Please tell me if it doesn’t work (it should), I have a B plan hehe.
EDIT: As reset doesn’t affect anchors, you can bind the reset event (B plan):
The setTimeout is necessary for a cross browser solution, because in some browsers, the reset event fires before elements actually reset. With that small delay, it’s done.
Cheers