retrieve select id value and get selected value from this select

1

Hello, my question is to store an id and a value of select and options in a variable.

I have this structure:

<div class="col-sm-6" id="results">
<select class="form-control" id="1">
    <option value="true">Sim</option>
    <option value="false">Não</option>
</select>
<select class="form-control" id="2">
    <option value="true">Sim</option>
    <option value="false">Não</option>
</select>
<select class="form-control" id="3">
    <option value="true">Sim</option>
    <option value="false">Não</option>
</select>
</div>

I need to get the select id and the value of the select option

    
asked by anonymous 05.09.2018 / 16:09

1 answer

1

You can do this if you want with jQuery:

$("select").on("change", function() {
  var id = $(this).prop("id");  // pega o id do select clicado
  var val= $(this).val();       // pega o valor do option selecionado
  
  console.log("ID:"+id+" - VALOR:"+val);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="col-sm-6" id="results">
  <select class="form-control" id="1">
      <option value="true">Sim</option>
      <option value="false">Não</option>
  </select>
  <select class="form-control" id="2">
      <option value="true">Sim</option>
      <option value="false">Não</option>
  </select>
  <select class="form-control" id="3">
      <option value="true">Sim</option>
      <option value="false">Não</option>
  </select>
</div>
    
05.09.2018 / 16:20