How to update select with jQuery

0

Hello. Home I have a select

<div class="seletor">
   <select name="numeros" id="numeros">
      <option value="1" selected="selected">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
   </select>
   <span class="custom-select">1</span>
</div>

And I wanted to know how do I select another value using jQuery?

    
asked by anonymous 22.05.2017 / 22:16

2 answers

2

You can use .val() directly in select:

$('#selectValue').click(function (){ 
  $('#numeros').val($('#valor').val());
  $('.custom-select').text($('#numeros').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="seletor">
   <select name="numeros" id="numeros">
      <option value="1" selected="selected">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
   </select>
   <span class="custom-select">1</span>
</div>
<br>
Digite o valor: <input type="text" id="valor">
<input type="button" id="selectValue" value="Selecionar" /><br>
    
22.05.2017 / 22:24
0

$('select option[value="2"]').attr({ selected : "selected"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select><optionvalue="1" selected>1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    
22.05.2017 / 22:26