JQuery insert element before <tr>

I am trying to insert a block element. Insert something in front of another. I'm not sure if I am using the correct method, but here is my code. Hope you guys can help. Thanks!

Jquery

$("#addMatch").click(function(){ $("<td>New insert</td>").insertBefore("#addMatch").closest('tr'); return false; //this would insert the <td>New insert</td> before the //<td><input type="button" id="addMatch" name="addMatch" value="Add //Match" </td> but not <tr> }); 

HTML

 <tr> <td>some data</td> </tr> //can't tell how many tr would show before the last "addMatch" button. It dynamic. // I want the <td>New insert</td> show up here. <tr> <td><input type="button" id="addMatch" name="addMatch" value="Add Match" </td> </tr> 
+6
jquery html insert
source share
1 answer

<td> should always be in <tr> .

I would probably do your function as follows:

 $("#addMatch").click(function(){ $(this).parents('tr:first').before('<tr><td>New Insert</td></tr>'); return false; }); 
+13
source share

All Articles