2

I am trying to select an item of dropdown by its text. I can't select them by value because value will be different everytime.

 $("#rn_SelectionInput_7_attend_session_time option").each( function(){ alert($(this).text()==radioValue);// this evaluate to true when a matching item is found if( $(this).text()==radioValue) { $("#rn_SelectionInput_7_attend_session_time").selectedIndex=$(this.index()); // this doesn't do anything } }); 

Thanks,

1 Answer 1

2

You can set the selected property of the option element to make it as selected

$("#rn_SelectionInput_7_attend_session_time option").filter(function () { alert($(this).text() == radioValue); // this evaluate to true when a matching item is found return $.trim($(this).text()) == radioValue }).prop('selected', true); 

Demo: Fiddle

  • filter() - to find the option element with the given text
  • .prop() - set the selected property to true
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the code. But it only works if I change prop to attr. Any ideas? is this browser specific?
@BishnuPaudel it is depending on jQuery version used... which is the jQuery version used
it's good if it's not browser specific. the page i am working now uses 1.4.2 and I can't change . Thanks,
@BishnuPaudel if you are using jQuery 1.4.2 then you need to use .attr(), if jQuery >= 1.6.4 then .prop()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.