How to set selected value as empty for dropdownlist via jquery

Viewed 87072

I am using jquery to populate dropdownlist, the codes are like below:

 var myOptions = {  
   val1 : 'text1',   
   val2 : 'text2',
   val3 : '' 
    };
 $.each(myOptions, function(val, text) {  
   $('#mySelect').append( $('<option></option>').val(val).html(text)    
);

 });

now I have populate 3 items in my dropdown, and now I want to set my dropdownlist selected value with '', just like:

$("#mySelect option contain('')").attr(selected,true).

Actually, this will get all 3 items so that it will not set '' as selected value, is there any way that i can do this?

3 Answers

What I am getting from your question is that you want to set the selected value as empty.
If so, you can try
$(“#elementId”).val('');
e.g.
$(“#val3”).val('');

otherwise if you want to set any specific value as selected by finding the option by its text, try this.

The last thing you might want to do as you mentioned above is that you want to set val3 as selected, i.e. setting a specific known option as selected, try this I have tried all these ways...Hope this helps :)

Related