Question

I need to get the option by the name or value of it and insert selected to it.

For example, I have this:

<select class="numbers_class">
<option value="">Select the Number</option>
<option value="1">Number 1</option>
<option value="2">Number 2</option>
<option value="3">Number 3</option>
<option value="4">Number 4</option>
<option value="5">Number 5</option>
</select>

And I need the jquery to find the "numbers_class" and let's say option with number 3 of it. And then set 'selected' attribute to it.

Was it helpful?

Solution

Get by name:

var option = $(".numbers_class option:contains('Number 3')");

Or get by value:

var option = $(".numbers_class option[value='3']");

Set selected attribute:

option.prop("selected", true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top