J. Davidson
J. Davidson

Reputation: 3307

Selecting value in dropdown as default

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

Upvotes: 1

Views: 386

Answers (6)

Lijo
Lijo

Reputation: 6778

    <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]

Upvotes: 0

Jason Loki Smith
Jason Loki Smith

Reputation: 438

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

Upvotes: 0

VenomVendor
VenomVendor

Reputation: 15392

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

Upvotes: 1

Bharat soni
Bharat soni

Reputation: 2786

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

$("#ddList option[value='North']").attr("selected","selected");

Upvotes: 0

chris342423
chris342423

Reputation: 451

One way:

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

Source: use jquery to select a dropdown option

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

Based on value:

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

BY text:

$("#ddList option").each(function() {
if($(this).text() == "North Side") {
  $(this).attr('selected', 'selected');            
  }                        
});

Upvotes: 4

Related Questions