Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the default value in dropdownlist using jQuery

People also ask

How can get default selected value of dropdown in jQuery?

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

How do I select a default dropdown value?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option.

How do I select the default selection in jQuery?

if your wanting to use jQuery for this, try the following code. $('select option[value="1"]'). attr("selected",true);

How set value in select?

Use the value property to set the value of a select element, e.g. select. value = 'new value' . The value property can be used to set or update the value of a select element. To remove the selection, set the value to an empty string.


if your wanting to use jQuery for this, try the following code.

$('select option[value="1"]').attr("selected",true);

Updated:

Following a comment from Vivek, correctly pointed out steven spielberg wanted to select the option via its Text value.

Here below is the updated code.

$('select option:contains("it\'s me")').prop('selected',true);

You need to use the :contains(text) selector to find via the containing text.

Also jQuery prop offeres better support for Internet Explorer when getting and setting attributes.

A working example on JSFiddle


You can just do this:

$('#myCombobox').val(1)

val() should handle both cases

  <option value="1">it's me</option>      


$('select').val('1'); // selects "it's me"

$('select').val("it's me"); // also selects "it's me"

$("#dropdownList option[text='it\'s me']").attr("selected","selected"); 

$('#userZipFiles option').prop('selected', function() {
        return this.defaultSelected;
    });     

jQuery("select#cboDays option[value='Wednesday']").attr("selected", "selected");