Acccess nth:tr or nth:td of table in Jquery | Techbirds
Some times we got a situation in which we have to access some specific table row
To access nth row
$(‘#firstTable tbody tr:nth-child(3).html(‘
To access nth table data
$(‘#first_Table tbody tr).find(‘td:nth-child(2)’);
$(‘#first_Table tbody tr).find(‘td:nth-child(2)’); |
To access each row of table
var rowCount = $(‘#first_Table tr’).length; //will count number of rows in table var i = 1; $(‘#first_Table tr’).each(function() { $(‘#first_Table tbody tr:nth-child(‘+i+’)’).find(‘td:eq(3)’).append(‘Some Text Goes Here’); i = i+1; });
var rowCount = $(‘#first_Table tr’).length; //will count number of rows in table var i = 1; $(‘#first_Table tr’).each(function() { $(‘#first_Table tbody tr:nth-child(‘+i+’)’).find(‘td:eq(3)’).append(‘Some Text Goes Here’); i = i+1; }); |
Count number of columns in HTML table
$(function() { var colCount = 0; $(‘tr:nth-child(1) td’).each(function () { if ($(this).attr(‘colspan’)) { colCount += +$(this).attr(‘colspan’); } else { colCount++; } }); });
$(function() { var colCount = 0; $(‘tr:nth-child(1) td’).each(function () { if ($(this).attr(‘colspan’)) { colCount += +$(this).attr(‘colspan’); } else { colCount++; } }); }); |
5,577 total views, 8 views today
Share this On