In general, there is a page where I work with ajax and jquery. On one half of the page there are 3 inputa where I drive the info, the second one contains the table where the previously entered information falls, that is, the table is dynamically generated.
The data entered in the input is sent by the Ajax to the PCP handler, which in turn pushes them into the database, and then forms a json file from the database (Do not ask why I am doing so, this is by condition). Then, with an Ajax, I read the json file and put it into a table on the page.
In general, here's the code:
<script type="text/javascript"> $(document).ready(function(){ //Тут я по клику собираю данные $("#send").click(function() { //из инпутов и отправляю в обработчик var firstName = $('#firstName').val(); var secondName = $('#secondName').val(); var email = $('#email').val(); $.ajax({ url: "core.php", type: "POST", data: {firstName,secondName,email} }); }); $.ajax({ //Тут я беру готовый json файл и вывожу url:"table_push.json", //в таблицу на странице dataType: "json", success:function() { $.getJSON('table_push.json',function (data) { for(var i = 0; i<data.length;i++){ $('#users').append('<tr><td>' + data[i].id + '</td><td>' + data[i].firstName + '</td><td>' + data[i].secondName + '</td><td>'+ data[i].email+'</td><td><button id='+data[i].id+' class="btn btn-danger">Remove</button></td></tr>'); } // В цикле сверху, я присваиваю id тегу <button>, $(".btn").bind('click', function () { // что б было var line = $(this).attr('id'); // удобно удалить alert(line); //строку из таблицы // а вот в этой функции сверху я проверяю через алерт })//правильность присвоенного id кнопке, выводит }); $(".btn").bind('click', function () { var line = $(this).attr('id'); alert(line); // а вот в этой функции я уже не могу обратится к id }) // на странице ничего не происходит и ошибок в консоли нету // по этому не могу напсать ещё один ajax запрос на удаление // данных из таблицы в БД } }); }); </script> The task is to bring information to the table, and then delete it by clicking on a specific entry in the table. But I can not do this, because I can not turn on the id to the record that needs to be deleted. If you look through the console in the browser, then all the buttons for deleting the entry are assigned id, but I can’t access them to manipulate the contents of the table in the future.