1

I have form with some text inputs and dropdown menus. This elements are generated dynamically based on the user selection in the previous page and this dynamic generation of elements are working fine. Now, let's say my code looks like this -

<form id="myForm" >
<select id='id1'>
<option value="">Select an option</option>  
<option value="opt1">opt1</option>  
<option value="opt2">opt2</option>  
<option value="opt3">opt3</option>  
</select>  
<select id='id2'>  
    <option value="">Select an option</option>  
    <option value="Apple">Apple</option>  
    <option value="Orange">Orange</option>  
    <option value="Grape">Grape</option>  
   </select>  
</form>

and now i wanted to reset the option of all the dropdown menu item to the first i.e. - "Select an option" when user clicks on reset button.

<button class="reset-field" id="clearBtn">Reset</button>


For this i tried-

$(document).ready(function(){
 ...
  $("#clearBtn").click(function(){
  $('#myForm select').each(function(key, value) {
   $(this).val("");
  });
 });
});

Basically this sets the value to "" but not the the option. I mean selected item is not getting changed. Any idea how we can set the option to - "Select an option".

Thanks.

1

4 Answers 4

1
$(this)[0].selectedIndex = 0;

Using the array selector on the jQuery element will return a pointer to the actual DOM based object (not the jQuery wrapped object). From there, you can just set selectedIndex to 0 (the 0th element in the list).

Sign up to request clarification or add additional context in comments.

3 Comments

Err.. Then why did OP's code not work..? $('select').val("")
Or just this.selectedIndex = 0;
Michael's comment was correct. just use this.selectedIndex. Missed that one < duh />
1

Try using .text("") instead of .val("")

$(document).ready(function(){
 ...
  $("#clearBtn").click(function(){
  $('#myForm select').each(function(key, value) {
   $(this).text("");
  });
 });
});

2 Comments

Then replace with .text("Select an option");
by doing this all other options will be removed only "Select an option" would be retained in the dropdown list which is not i am trying to do.
0

(Not an answer strictly speaking, just a thought)

val() concept in jQuery is quite rudimentary.

Check this discussion where I am proposing the formation mechanism. With it your task can be accomplished as

$("#myForm").formation().value = { id1:"", id2:"" };

Essentially, the formation introduces value of the form as name/value collection of its inputs. It correctly handles values of selects, radio groups and checkboxes.

Comments

0

I think this is what you are looking for:

http://jsfiddle.net/YZWTY/

$(function(){
        $("#clearBtn").click(function(e){
         e.preventDefault();
            $("#myForm select option").each(function(){
               var a = $(this).index();
               $(this).val(a);   
              });
            $("#myForm select").val("0");
        });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.