Get id of a dynamic select with jquery

Asked

Viewed 1,016 times

3

I need to get the id Bairro3, I’m doing like this

$(document).on('change', '#bairro', function(){
        var bairroID = $(this).val(); 

but it still doesn’t work.

You have to add a variable in the #neighborhood, for each form that opens has a different id #bairro1 #bairro2 #bairro3..., how can I do this, I imagine I would have to do this for the id #rua also #rua1 #rua2 #rua3...

$(document).ready(function(){

    $(document).on('change', '#sectionChooser', function(){
        var myID = $(this).val();
        $('.panel').each(function(){
            myID === $(this).attr('id') ? $(this).show() : $(this).hide();
            console.log("ok");
        });
    })


    $(document).on('change', '#bairro', function(){
        var bairroID = $(this).val();

        if(bairroID){
            $.ajax({
                type:'POST',
                url:'ajax_data2.php',
                data:'bairro_id='+bairroID,
                success:function(html){
                    $('#rua').html(html);
                    console.log("ok");
                }
            }); 
        }else{
            $('#rua').html('<option value="">Select categoria first</option>');

        }
    });

});

Html

<div class="col-sm-6">
<label for="Planos">Planos</label>
<select name="Planos" id="sectionChooser" class="form-control valid" aria-invalid="false">
   <option value="">Selecione</option>
   <option value="1">Diamante</option>
   <option value="2">Ouro</option>
   <option value="3">Prata</option>
   <option value="4">Light</option>
   <option value="5">Free</option>
   <option value="19">Link</option>
</select>
<div class="panel" id="1" style="display: block;">
   <div class="col-sm-12">
      <label><b>Diamante</b></label>
   </div>
</div>
<div class="col-sm-6">
   <label for="Bairro">Bairro</label>
   <select class="form-control valid" name="bairro" id="bairro3" required="" aria-invalid="false">
      <option value="">Selecione</option>
      <option value="1">teste bairro</option>
      <option value="2">teste bairro 2 3</option>
      <option value="3">ação bairros</option>
      <option value="4">Centro</option>
   </select>
</div>
<div class="col-sm-6">
   <label for="Rua">Rua</label>
   <select class="form-control" name="rua" id="rua" required="">
   </select> 
</div>
  • 1

    As I said in the chat, You do not need to create several ids... just create Names with arrays, type name="neighborhood[]"... and you can get the value of each one with $(document).on('change', '[name^="bairro"]', function(){ var bairroID = $(this).val();

  • Not looking too deeply into your question, growing id’s almost never (if ever) is what you want, but rather classes (all with the same), or a solution like @dvd indicated.

  • I think you could have focused on the other question which is almost the same thing as this, then you would have solved it all

  • It is obvious that if you use the selector by ID as here $(document).on('change', '#bairro' the result will always be "neighborhood". Why not make a selector by name, or tag type ( select, input, etc) or per class?

1 answer

0

That’s the code, it worked

$(document).ready(function(){

    $(document).on('change', '#sectionChooser', function(){
        var myID = $(this).val();
        $('.panel').each(function(){
            myID === $(this).attr('id') ? $(this).show() : $(this).hide();
            console.log("ok");
        });
    })


   $(document).on('change', '[name^="bairro"]', function(){ var bairroID = $(this).val();
        console.log("ok");

        if(bairroID){
            $.ajax({
                type:'POST',
                url:'ajax_data2.php',
                data:'bairro_id='+bairroID,
                success:function(html){
                    $('#rua[name^="rua"]').html(html);

                }
            }); 
        }else{
            $('#rua').html('<option value="">Select categoria first</option>');

        }
    });

});

Browser other questions tagged

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