Agyan
adda
Run
Reset
Copy
jQuery Tutorial › jQuery Event Delegation › Editable student table
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <style>td,th{border:1px solid #ccc;padding:6px} table{border-collapse:collapse;margin-top:10px} tr.done{background:#ebfbee}</style> <input id="student" placeholder="Student name"> <button id="add">Add student</button> <table id="table"> <tr><th>Name</th><th>Action</th></tr> <tr><td>Asha</td><td><button class="mark">Present</button> <button class="delete">Delete</button></td></tr> </table> <p>Present: <span id="count">0</span></p> <script> $(function () { $('#add').click(function () { const name = $.trim($('#student').val()); if (!name) return; const $row = $('<tr>').append($('<td>').text(name)) .append('<td><button class="mark">Present</button> <button class="delete">Delete</button></td>'); $('#table').append($row); $('#student').val(''); }); $('#table').on('click', '.mark', function () { $(this).closest('tr').toggleClass('done'); $('#count').text($('#table tr.done').length); }); $('#table').on('click', '.delete', function () { $(this).closest('tr').remove(); $('#count').text($('#table tr.done').length); }); $('#student').on('keyup', function (e) { if (e.key === 'Enter') $('#add').trigger('click'); }); }); </script>
Result