Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - remove table row <tr> by clicking a <td>
    primarykey
    data
    text
    <p>I am making a table in which you can add aditional rows. When you add a row you can either save it or cancel it, by clicking cancel the row will be removed. It works with one row but when I create like six of them and click cancel the selected row wont be removed but the last row will. Here my Code so far. Does anyone know what I'm doing wrong? </p> <pre><code>&lt;head&gt; &lt;script src="../jquery.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $(".edit").click(function() { var id = $(this).attr("id"); alert("edit "+id); }); $(".delete").click(function() { var id = $(this).attr("id"); alert("delete "+id); }); $("#newbutton").click(function() { var randomnumber=Math.floor(Math.random()*100); $("tr:last").after("&lt;tr id="+randomnumber+"&gt;&lt;td&gt;&lt;form&gt;&lt;input style='width: 80%'&gt;&lt;/form&gt;&lt;/td&gt;&lt;td class=ok&gt;OK&lt;/td&gt;&lt;td id="+randomnumber+" class=cancel&gt;Cancel&lt;/td&gt;&lt;/tr&gt;").ready(function() { $("tr td .cancel").click(function() { $(this).remove(); }); $(".ok").click(function() { alert("OK!"); }); }); }) }); &lt;/script&gt; &lt;/head&gt; &lt;table border=1 id=table&gt; &lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Bombai&lt;/td&gt;&lt;td id=1 class=edit&gt;edit&lt;/td&gt;&lt;td id=1 class=delete&gt;delete&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;London&lt;/td&gt;&lt;td id=2 class=edit&gt;edit&lt;/td&gt;&lt;td id=2 class=delete&gt;delete&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;Rom&lt;/td&gt;&lt;td id=3 class=edit&gt;edit&lt;/td&gt;&lt;td id=3 class=delete&gt;delete&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;&lt;label id=newbutton&gt;New Place&lt;/label&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload