Reputation: 31
How do you reset the values of various types of form elements that are in a div when you use jQuery show/hide?
Upvotes: 3
Views: 1438
Reputation: 5018
Resetting the values of form elements is going to be no different if they are hidden. Hide/show only change the display attribute, the elements still exist in the DOM.
Upvotes: 0
Reputation: 2940
If memory serves, show/hide toggles the display attribute only. One should be able to modify the DOM in the same manner regardless of whether the elements are shown or not.
Upvotes: 0
Reputation: 38336
I am not sure if I get your question correctly, but the following jQuery magic will find your <div>
element by id
, hide it, find all <input>
controls within it and "reset" their values to empty string.
$("#myDiv").hide().find("input").val("");
If you need to target other form elements than <input>
(such as <select>
or <textarea>
) you will need to add selectors for these as well. If you need to reset to other values (possible values provided in the original markup from the server), you will need to cache these values before the user gets to fiddle with the data in the form.
Upvotes: 5