How can I reset multiple select boxes in a form with jquery?
I have this so far, it resets everything but the selects.
$('button#reset').click(function(){
$form = $('button#reset').closest('form');
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
$form.find('select').selectedIndex = 0;
});
Added some markup:
<form id="form" name="form" method="post" class="form" role="form" action="inventory-search" enctype="multipart/form-data">
<div class="form-group">
<label for="grade">Grade</label>
<select name="grade" class="form-control input-sm" onchange="this.form.submit();">
<option value="">Any</option>
<option value="opt1">opt1</option>
<option value="opt2" selected="selected">opt2</option>
</select>
</div>
<!-- there are 6 more select controls -->
<div class="form-group">
<label> </label>
<button type="submit" name="search" id="search" value="search" class="btn button-sm btn-primary">Search</button>
</div>
<div class="form-group">
<label> </label>
<button type="reset" name="reset" id="reset" value="reset" class="btn button-sm btn-primary">Reset</button>
</div>
</form>
$('select[multiple]'). multiselect('reload') needs to reset the select options.
reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method.
JQuery doesn't have a reset() method, but native JavaScript does. So, we convert the jQuery element to a JavaScript object. JavaScript reset(): The reset() method resets the values of all elements in a form (same as clicking the Reset button).
jQuery multiselect is an easier and highly customizable substitute for the standard <select> with the multiple boxes. This is basically a plugin that is more user-friendly and easy to implement when it comes to multiple selections of the checkboxes.
This will work:-
$form.find('select').prop('selectedIndex',0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With