<...">

Jquery setting option selected with known value

I have the following choice:

<select name="priority" id="priority" class="update_priority">
    <option value="root" label="Without parent">Without parent</option>
    <option value="72" label="Rank3">Rank3</option>
    <option value="71" label="Rank1">Rank1</option>
    <option value="67" label="Rank2">Rank2</option>
    <option value="64" label="Rank4">Rank4</option>
</select>

In JS, I have a variable with something value. For instance:

selected = 71;

Now, using jQuery, I want the option to be selected with this value (in our example 71).

+3
source share
1 answer

you do not need jquery for this. you can just use plain old javascript:

document.getElementById("priority").value = "71";

But if you still want to use jquery, you can do the same:

$("#priority").val("71")

EDIT: Here is an example. You can comment on one of them that you want to see for the other to work: http://jsfiddle.net/BupWA/

+14
source

Source: https://habr.com/ru/post/1761477/


All Articles