Allerson
Allerson

Reputation: 109

how to set an option being selected by jquery in ie?

I try to set an option being selected via its value.My code is out here.It works in FF and Chrome but not in IE.can anyone help me?

var province = $("#hideProvince").val();
if(province != ""){
  $("#province option[value='"+ province + "']").attr("selected", "selected");
}

Upvotes: 4

Views: 3328

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

You can set val() on the select to update the selected option:

var province = $("#hideProvince").val();
if (province != ""){
  $("#province").val(province)
}

Example fiddle

Upvotes: 2

Adil
Adil

Reputation: 148110

Try attr("selected", true) instead of attr("selected", "selected"), I found this post it may be related to what you are looking for.

var province = $("#hideProvince").val();
if(province != ""){
  $("#province option[value='"+ province + "']").attr("selected", true);
}

Upvotes: 1

Related Questions