Filter Element Option

Asked

Viewed 47 times

1

How do I get the object option with Jquery specifically this:

$('#nome_responsavel').blur(function () {
   var id = $('option[value="'+$('#nome_responsavel').val()+'data-toggle=p"]').attr('id');
   alert(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<label>Nome</label>
<input type="text" class="form-control" name="nome_responsavel" list="list_nome" id="nome_responsavel" required>
<datalist id="list_nome">
<option id="1" value="teste" data-toggle="p">teste</option>
<option id="2" value="teste2" data-toggle="p">teste2</option>
</datalist>

1 answer

2


Use:

$('#list_nome option')  // Captura os dois options dentro do #list_nome

If you want to capture what you possess value same as the text box is doing right, but use only one attribute per square bracket, if you want two attributes, use two square brackets:

$('#list_nome option[value="'+$('#nome_responsavel').val()+'"][data-toggle="p"]')  // Captura os dois options dentro do #list_nome
  • Would you like to put the data-toogle in this selection is possible? Because I have two data list on the page. And if you both have some equal value it might be a problem.

  • @Guilhermecosta, look at the edition. See if it works

  • It worked top. all select options is between []?

  • @Guilhermecosta, Whenever you want to select an attribute, yes, it is.

Browser other questions tagged

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