Skip to content Skip to sidebar Skip to footer

Reset Value Of Multiple (but Not All) Form Fields With Jquery In One Line

Is it possible to reset multiple form fields in one line, or one hit, with jQuery. Note: I don't want to reset all form fields, only a specified whitelist (as below): // reset some

Solution 1:

It is better to use a class so you do not have to maintain a long list of ids.

HTML

<inputtype="text"class="resetThis"id="address11" />
<inputtype="text"class="resetThis"id="address21" />

JavaScript

$(".resetThis").val("");

Solution 2:

jQuery (and CSS) selector strings can contain multiple selectors using a comma as a delimiter for sub-selectors:

$('#address11, #address21, #town1, #county1, #postcode1').val('');

I'd argue that this is faster than using a class (ID look-ups should perform in essentially constant time, whereas a class look-up will have to visit every DOM node), but perhaps less maintainable if you're going to want to change which elements get reset.

Solution 3:

If you have a lot of fields i'd label the ones you want to ignore with a class to minimise code:

$('#myForm input:not(.ignore)').val('');

Solution 4:

You can use $('#Form_name select:not(.fixed)').val('');

Post a Comment for "Reset Value Of Multiple (but Not All) Form Fields With Jquery In One Line"