JQuery adds "selected" to an option

0

I have the following code:

<select id="billing">
    <option value="485">Acre</option>
    <option value="486">Amapá</option>
    ...
</select>

When the user types the zip code, the system automatically searches for the address and inserts the data into the form. However, I need this JQuery to find the value of the option and include selected="selected". Thanks for the strength.

    
asked by anonymous 23.05.2017 / 18:59

1 answer

1

Not having the rest of the code, it's hard to give a working example, but this should do what you need: $('#billing').val(seuValor);

function testar(){
  $('#billing').val('485');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><selectid="billing">
    <option value="" disabled selected hidden>Estado</option>
    <option value="485">Acre</option>
    <option value="486">Amapá</option>
</select>
<button onclick="testar()">Clique me</button>
    
23.05.2017 / 19:01