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?
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.
Use $('select[id="salesrep"]'). val() to retrieve the selected value. Use $('select[id="salesrep"]'). val("john smith") to select a value from dropdown.
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'); });
$("#mySelect option[value='']").attr('selected', true)
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