block of code only works with F5

0

Could someone tell me why this block of code only works after refreshing the page?

var count = 0;

$( document ).ready(function() {

//  alert("testando bloco");
  $("#select_n").attr('id', 'select_' + count);

  $("#add").click(function(){
    count ++;

    // desnecessario
    $("#select_n").attr('id', 'select_' + count);

    //$("#filtro_subdisciplinas_select").attr('id', 'subdisciplinas_select_' + count);
    $("#subdisciplina_select_n").attr('id', 'subdisciplina_select_' + count);
    //$("#carro_div_n").attr('id', 'carro_div_' + carro_temp);

  });
});
    
asked by anonymous 01.12.2016 / 22:02

1 answer

1

You are referring $("#select_n") and changing your id to select_' + count . So within click it tries to refer to the new id. $("#select_"+(count-1)) .

var count = 0;

$( document ).ready(function() {
   //  alert("testando bloco");
   $("#select_n").attr('id', 'select_' + count);

   $("#add").click(function(){
        count ++;

        // desnecessario
        $("#select_"+(count-1)).attr('id', 'select_' + count);

        //$("#filtro_subdisciplinas_select").attr('id', 'subdisciplinas_select_' + count);
        $("#subdisciplina_select_"+(count-1)).attr('id', 'subdisciplina_select_' + count);
        //$("#carro_div_n").attr('id', 'carro_div_' + carro_temp);
    });
 });
    
02.12.2016 / 11:58