Reputation: 23
I have a problem where the :eq()
does not accept the counter, n
, to insert values to a new column to the table in the HTML.
$(document).ready(function() {
var $tablerow = $('table.table').find('tr');
count = 0;
$tablerow.each(function(index, value){
count += 1;
var $listitem = $(this);
n = parseInt($listitem.index());
var $newRow = $("<td>" + n + "</td>");
$("table.table tr:eq(n)").append($newRow);
});
});
HTML
<table class="table">
<tr><td>First row</td></tr>
<tr><td>second row</td></tr>
<tr><td>third row</td></tr>
<tr><td>fourth row</td></tr>
<tr><td>fifth row</td></tr>
<tr><td>sixth row</td></tr>
<tr><td>seventh row</td></tr>
<tr><td>eighth row</td></tr>
</table>
Upvotes: 2
Views: 2718
Reputation: 14628
Replace
var $newRow = $("<td>" + n + "</td>");
$("table.table tr:eq(n)").append($newRow);`
with
var $newRow = $("<td>" + (n+1) + "</td>");
$("table.table tr:eq(" + n + ")").append($newRow);
This will work
Upvotes: 0
Reputation: 171679
No need to index this
inside $.each
since as your arguments show the first one is index.
$(document).ready(function() {
var $tablerow = $('table.table tr');
$tablerow.each(function(index,element){
/* "this" is current row*/
$(this).append("<td>" + index+1 + "</td>" )
});
});
Upvotes: 0
Reputation: 108
May be try using it like previous line where you used qoutes
$("table.table tr:eq(" + n + ")").append($newRow);
Upvotes: 0
Reputation: 21191
Written as-is, you're doing nothing more than writing a literal character, 'n', into .eq()
method. Try this:
$("table.table tr:eq(" + n + ")").append($newRow);
Upvotes: 1