Tutorial :jQuery show/hide: How do you reset the value of a form element that is inside a show/hide div?



Question:

How do you reset the values of various types of form elements that are in a div when you use jQuery show/hide?


Solution:1

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.


Solution:2

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.


Solution:3

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »