1

Hi I am trying to select on load a certain option from dropdown to show as selected option(blue background). My jquery code is

$(document).ready(function () {
   //ddLiss it he id of the dropdown
   //North Side is one of the options that i need selected on document load. 
  $("#ddList").select("North Side");
});

When i run this in code, I get the drop down with all values but "North Side" doesn't show as selected one. Please let me know how I can fix it. Thanks

J. Davidson
  • 3,297
  • 13
  • 54
  • 102

6 Answers6

4

Based on value:

$('#ddList').val('North Side');

BY text:

$("#ddList option").each(function() {
if($(this).text() == "North Side") {
  $(this).attr('selected', 'selected');            
  }                        
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1

$("#ddList option:contains('North Side')").prop("selected",true);

VenomVendor
  • 15,064
  • 13
  • 65
  • 96
0

One way:

$('select>option:eq(3)').attr('selected', true);

Source: use jquery to select a dropdown option

Community
  • 1
  • 1
chris342423
  • 439
  • 1
  • 6
  • 22
0

Try this and set the value "North" in your dropdown.

$("#ddList option[value='North']").attr("selected","selected");
Bharat soni
  • 2,686
  • 18
  • 27
0

All dropdowns have a "SelectedValue" and a "SelectedText" option.

You have to set the value of the dropdown to the value of "North Side".

You can do this via:

$("#ddList").val("1"); // '1' being the value of the bound list

Jason Loki Smith
  • 428
  • 3
  • 12
0
    <select>
        <option>please select</option>
        <option value='1'>northside</option>
        <option value='2'>south</option>
    </select>
$(document).ready(function(){
    $('select option:contains("northside")').prop('selected',true);
});

check this fiddle [check this fiddle][1]

Lijo
  • 6,498
  • 5
  • 49
  • 60