Reputation: 609
How to do dynamic html table on JS with jQuery? For example I must have 6 buttons:
UPD:
That's my JS:
$(document).ready(function(){
$('#addFirstPosition').click(function(){
var $tr = $('<tr><td>3</td><td>3</td></tr>');
//var $myTable = $('#myTable');
//$myTable.append($tr);
$("#myTable > tbody").append($tr);
);
});
And this is my html:
<input id="addFirstPosition" type="button" value="AddFirst" />
<input id="addMiddlePosition" type="button" value="AddMiddle" />
<input id="addLastPosition" type="button" value="AddLast" />
<br />
<input id="deleteFirstPosition" type="button" value="DelFirst" />
<input id="deleteMiddlePosition" type="button" value="DelMiddle" />
<input id="deleteLastPosition" type="button" value="DelLast" />
<br />
<br />
<table id="myTable" border="1px">
<tbody>
<tr>
<td>
1
</td>
<td>
1
</td>
</tr>
<tr>
<td>
2
</td>
<td>
2
</td>
</tr>
</tbody>
</table>
When I click to button, nothing happens.
Upvotes: 0
Views: 3597
Reputation: 17666
There were errors in your js code:
$(document).ready(function() {
$('#addFirstPosition').click(function() {
var $tr = $('<tr><td>3</td><td>3</td></tr>');;
$("#myTable > tbody").append($tr);
});// I WAS MISSING A } BEFORE );
});
brackets missing or in the wrong places. The code above is corrected and I commented on the mistake.
but Parv Sharma had some good points in his/her answer.
Upvotes: 2
Reputation: 12705
going by the format in which u have asked this ques search for the rows by doing a $(tr); then use
.append(); for adding to the last
.prepend(); for adding to the first
select an elemnt by doint $(tr).eq(index).after() to add in between
after you have selected the row which you can do by
var r = $(tr).eq(index)
to delete you can do
r.remove()
no matter where you row is present
Upvotes: 6