Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Jquery to reset a select box

I'm having difficulty with using jQuery to reset a value of a select box. My code is as follows:

HTML:

<form id="myform">
  <select name="myselect" id="myselect" value=""> 
    <option value="">Default</option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
  </select>  
  <input type="submit" value="Go">
  <input type="reset" class="reset" value="Reset form">  
</form>

Javascript:

​$("#myform .reset").click(function() {
    $('#myselect').attr('value','');
    alert($('#myselect').attr('value'));
});​

As you can see option 2 is selected, but I want to reset the select box to "Default". I've tried various methods including attr->selectedIndex but all have the same affect, it briefly changes to Default (As can be seen if you look at the form when the "Alert box" pops up, but as soon as the alert box is closed, or the alert line removed, it jumps back to the currently selected option 2.

I think this may be a jQuery bug, I've tried many different versions, from 1.6 to edge, all with same effect.

For ease I've added a jsfiddle: http://jsfiddle.net/ux2f2/1/

Hope I've included everything as this is my first post, but im a long time reader :) Love this site!

like image 945
Mark Avatar asked Oct 22 '12 13:10

Mark


People also ask

How do I empty a select field in jQuery?

To remove all options from a select list using jQuery, the simplest way is to use the empty() method.

How can get default selected value of dropdown in jQuery?

$('select option:selected'). val(); will always give the current dropdown selected value.

How do you clear a selection in HTML?

To remove the options of an HTML element of select , you can utilize the remove() method: function removeOptions(selectElement) { var i, L = selectElement.

How do I remove a selected value from a Dropdownlist?

The option to be removed is selected by getting the select box. The value to be removed is specified on the value selector (value='optionValue') on the select box. The remove() method is then used to remove this selected option. The find() method can be used to find the option in the value with the value selector.


1 Answers

You don't need any javascript to reset this simple form. Just use the attribute selected in your (X)HTML

<form id="myform">
  <select name="myselect" id="myselect" value=""> 
    <option selected="selected">Default</option>
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
  </select>  
  <input type="submit" value="Go">
  <input type="reset" class="reset" value="Reset form">  
</form>

If you really want to use jQuery, we can do this :

$("#myform .reset").click(function() {
    $('#myselect').prop('selectedIndex', -1);
});

the value "-1", means "reset"

like image 109
mquandalle Avatar answered Sep 21 '22 02:09

mquandalle