recover id value from select and get the selected value from that select

Asked

Viewed 883 times

1

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

have that 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 take the id of the select and the option value of that select

1 answer

1


You can do it like that 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>

<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>

  • Hello Leandrade, I will test friend...

  • It worked out Andrade, thank you

  • Nice Jose, for nothing!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.