Adding a table row in jQuery
The approach you suggest is not guaranteed to give you the result you're looking for - what if you had a tbody
for example:
<table id="myTable">
<tbody>
<tr>...</tr>
<tr>...</tr>
</tbody>
</table>
You would end up with the following:
<table id="myTable">
<tbody>
<tr>...</tr>
<tr>...</tr>
</tbody>
<tr>...</tr>
</table>
I would therefore recommend this approach instead:
$('#myTable tr:last').after('<tr>...</tr><tr>...</tr>');
You can include anything within the after()
method as long as it's valid HTML, including multiple rows as per the example above.
Update: Revisiting this answer following recent activity with this question. eyelidlessness makes a good comment that there will always be a tbody
in the DOM; this is true, but only if there is at least one row. If you have no rows, there will be no tbody
unless you have specified one yourself.
DaRKoN_ suggests appending to the tbody
rather than adding content after the last tr
. This gets around the issue of having no rows, but still isn't bulletproof as you could theoretically have multiple tbody
elements and the row would get added to each of them.
Weighing everything up, I'm not sure there is a single one-line solution that accounts for every single possible scenario. You will need to make sure the jQuery code tallies with your markup.
I think the safest solution is probably to ensure your table
always includes at least one tbody
in your markup, even if it has no rows. On this basis, you can use the following which will work however many rows you have (and also account for multiple tbody
elements):
$('#myTable > tbody:last-child').append('<tr>...</tr><tr>...</tr>');
How to add rows in HTML Table using Jquery?
Your input string HTML is incorrect. As of now you have no TD
element thus content is not displayed. However its appended and exists in DOM
'<tr><td>Record1</td><td>Record2</td></tr>
instead of
'<tr>Record1</tr><tr>Record2</tr>'
$('#queryTable > tbody:last-child').append('<tr><td>Record1</td><td>Record2</td></tr>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover" id="queryTable">
<thead>
<tr>
<th>Field Name</th>
<th>Values</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mark</td>
<td>Otto</td>
</tr>
<tr>
<td>Jacob</td>
<td>Thornton</td>
</tr>
</tbody>
</table>
jQuery: Adding rows to a table
You started with a </tr>
closing tag.
$('#vorschlaege > tbody:last-child').append(
'<tr>'// need to change closing tag to an opening `<tr>` tag.
+'<td><input type="checkbox" checked="true"></td>'
+'<td>'+name+'</td>'
+'<td>'+fvv+'</td>'
+'<td><button id="loeschen">löschen</button></td>'
+'</tr>');
How to add table rows to html table using JQuery
Your code is fine. Just append to tbody.
$(document).ready(function () {
var tbl = $('#mytable');
appendTableRow(tbl)
});
function appendTableRow(table) {
var newrow = '';
newrow += "<tr>";
newrow += "<td>21</td>";
newrow += "<td>22</td>";
newrow += "</tr>";
$('tbody').append(newrow);
}
table {
border-collapse: collapse;
}
table td {
border: 1px solid black;
}
table th {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable">
<thead>
<th>Col1</th>
<th>Col2</th>
</thead>
<tbody>
<tr>
<td>11</td>
<td>12</td>
</tr>
</tbody>
</table>
Add row to the beginning of a table - JavaScript - jQuery
http://jsfiddle.net/32Ymw/
$("#but").click(function(){
row = $("<tr></tr>");
col1 = $("<td>col1</td>");
col2 = $("<td>col2</td>");
col3 = $("<td>col3</td>");
row.append(col1,col2,col3).prependTo("#mytable");
});
Dynamically add table row number using jquery
There are lots of ways to do this. As I post this, I see 3 viable options already posted. Here is what I would suggest.
$(function() {
function numberRows($t) {
var c = 0;
$t.find("tr").each(function(ind, el) {
$(el).find("td:eq(0)").html(++c + ".");
});
}
$("#add-service-button").click(function(e) {
e.preventDefault();
var $row = $("<tr>");
$row.append($("<td>"));
$row.append($("<td>").html($("#add-service").val()));
$row.append($("<td>").html("<a class='del-service' href='#' title='Click to remove this entry'>X</a>"));
$row.appendTo($("#service-table tbody"));
numberRows($("#service-table"));
});
$("#form-entry form").submit(function(e) {
e.preventDefault();
$("#add-service-button").trigger("click");
});
$("#service-table").on("click", ".del-service", function(e) {
e.preventDefault();
var $row = $(this).parent().parent();
var retResult = confirm("Are you sure you wish to remove this entry?");
if (retResult) {
$row.remove();
numberRows($("#service-table"));
}
});
});
.form-entry input {
border: 1px solid #ccc;
width: 75%;
line-height: 2em;
padding-left: 1em;
}
.form-entry button {
border: 0;
background-color: #5AD0A1;
color: #fff;
cursor: pointer;
padding: .6em 1.25em;
}
.table-wrapper {
font-family: Arial, sans-serif;
}
.table-wrapper table td {
border-bottom: 1px dashed #ccc;
padding: 1em .2em;
}
.table-wrapper table tr:last-child td {
border-bottom: 0;
}
.table-wrapper table td:first-child {
color: #ccc;
width: 2em;
}
.table-wrapper table td:last-child a {
color: #F00;
text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-entry">
<form>
<input type="text" id="add-service" placeholder="Enter your service name here..." />
<button type="submit" id="add-service-button">Add</button>
</form>
</div>
<div class="table-wrapper">
<table id="service-table" width="100%">
<tbody>
</tbody>
</table>
</div>
How do you append rows to a table using jQuery?
I'm assuming you want to add this row to the <tbody>
element, and simply using append()
on the <table>
will insert the <tr>
outside the <tbody>
, with perhaps undesirable results.
$('a').click(function() {
$('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
});
EDIT: Here is the complete source code, and it does indeed work: (Note the $(document).ready(function(){});
, which was not present before.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a').click(function() {
$('#myTable tbody').append('<tr class="child"><td>blahblah</td></tr>');
});
});
</script>
<title></title>
</head>
<body>
<a href="javascript:void(0);">Link</a>
<table id="myTable">
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</body>
</html>
how to add the table row after the given id in jquery
You can use after()
instead of append()
DEMO
$('#tableclick').click(function() {
$('#tr_second').after('<tr><td>test1</td><td>test2</td><td>test3</td></tr>');
});
Adding rows to tbody of a table using jQuery
I have never ever come across such a strange problem like this! o.O
Do you know what the problem was? $ isn't working. I tried the same code with jQuery like jQuery("#tblEntAttributes tbody").append(newRowContent);
and it works like a charm!
No idea why this strange problem occurs!
Related Topics
Why Does Changing an Array in JavaScript Affect Copies of the Array
How to Access Post Form Fields in Express
Firebase Kicks Out Current User
Programmatically Navigate Using React-Router
Is It Safe to Expose Firebase APIkey to the Public
Filter Array of Objects Based on Another Array in JavaScript
Convert String to Title Case With JavaScript
How to Loop Through a Plain JavaScript Object With the Objects as Members
Passing Data to a Bootstrap Modal
Generate Unique Random Numbers Between 1 and 100
Jquery Selector Regular Expressions
How to Reverse a String In-Place in JavaScript