There is a table that is partially created

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <table class="table table-striped table-bordered table-hover" id="table"> <tr> <td><b>#</b></td> <td><b>Фамилия</b></td> <td><b>Имя</b></td> <td><b>Отчество</b></td> </tr> </table> 

And there is javascript

 <script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "POST", url: "DataTable", contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (data, textStatus, jqXHR) { $.each(data, function (i) { $("#table").append("<tr id = '" + (i+1) + "'> <td>" + (i + 1) + "</td> <td>" + data[i].Surname + "</td> <td>" + data[i].Name + "</td> <td>" + data[i].Lastname + "</td> </tr>"); i++; }); } }); $('table tr').bind('click', function () { alert("dsfsdf111111"); }); }); $('table tr').bind('click', function () { alert("dsfsdf111111"); }); $("#4").on("click", function () { alert("dsfsdf"); }); </script> 

When I click on the created append lines, nothing happens, and when I click on the line that was originally created by the html code, the alert function is called. How to make the event handler trigger when clicking on the created lines?

And how can I specify that when I click on the identifier from 1 to .... the onclick handler is called?

    1 answer 1

    Use this approach:

     $(document).on("click", '.mylink', function(event) { alert("new link clicked!"); }); 

    (c) from

    • And how to make the corresponding onclick called for each line? - DeVix
    • Are you .mylink about replacing .mylink with table tr ? ... for God's sake ... - borodatych
    • This is understandable, I’m talking about adding $ ('Identifier from 1 to ...'). Bind ('click', function () {alert ("dsfsdf111111");}); To not write 100500 onclick? - DeVix