Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selected value as empty for dropdownlist via jquery

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?

like image 851
Iori-kyo Avatar asked Jul 18 '11 14:07

Iori-kyo


People also ask

How can I change the selected value of a Dropdownlist using jQuery?

Using the jQuery change() method; you can set the selected value of dropdown in jquery by using id, name, class, and tag with selected html elements; see the following example for that: Example 1 :- Set selected value of dropdown in jquery by id.

How show selected value from database in dropdown using jQuery?

Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.

How remove selected attribute from option in jQuery?

If it is a multi-select and you need to remove the selected from all rows, you can use the following: $("#mySelect option:selected"). each(function () { $(this). removeAttr('selected'); });


1 Answers

$("#mySelect option[value='']").attr('selected', true)
like image 117
Tim Büthe Avatar answered Oct 21 '22 04:10

Tim Büthe