New elements do not work a function - Jquery

Asked

Viewed 26 times

0

Good afternoon! I don’t have much experience with jquery, so I’m having problems. I’m creating a new element when I click on a button.

$('#tbody_6').append('<tr id="'+token+'"><td><input required id="time_ini_6['+token+']" class="form-control" type="time" name="time_ini_6['+token+']"><input required class="form-control" type="time" id="time_max_6['+token+']" name="time_max_6['+token+']"><input id="price_6['+token+']" name = "price_6['+token+']"type="text" class="form-control" placeholder="Preço" value="" autofocus><button class="btn btn-md btn-alt btn-danger" id="minus_'+token+'" onclick="removePeriod(this)" type="button"><i class="fa fa-minus-circle"></i></button></td></tr>');

Creating something more or less like this:

inserir a descrição da imagem aqui

After that I wanted to call a function to return the id of this my removal button.

        function removePeriod(elem){
            var id = $(elem).attr("id");
            console.log(id);    
    }

But this isn’t happening, I always get an errorinserir a descrição da imagem aqui

  • That function removePeriod is defined in the global scope? It seems to me that your element is not able to see the function.

  • It was set within $(Document). ready(Function().... Thus, it could not find. I just left inside the script tags and took it from inside Document.ready and it worked for me. Thanks

No answers

Browser other questions tagged

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