Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQUERY: How do I select an option by value? [duplicate]

Tags:

jquery

<tfoot>
    <select>
        <option value="222"></option>
        <option value="2222">2222</option>
        <option value="zaza">zaza</option>
    </select>
</tfoot>

I tried that:

$('tfoot select').val('zaza');

$("tfoot select").val("zaza")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<tfoot>
    <select>
        <option value="222"></option>
        <option value="2222">2222</option>
        <option value="zaza">zaza</option>
    </select>
</tfoot>

Any idea ? Select = make it selected = trigger the change event

like image 548
yarek Avatar asked Nov 23 '16 21:11

yarek


2 Answers

You can use:

$('option[value="222"]')

In general,

$("tag[attr=value]") will search a tag with attributte attr and value value

To select it,

$('option[value="222"]').prop("selected", true);
like image 109
rupps Avatar answered Oct 20 '22 19:10

rupps


$(function () {
  $("#clientList").children('[value="zaza"]').attr('selected', true);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<tfoot>
    <select id="clientList">
        <option value="222">222</option>
        <option value="2222">2222</option>
        <option value="zaza">zaza</option>
    </select>
</tfoot>
like image 37
Daniel Avatar answered Oct 20 '22 17:10

Daniel