Get <td> element value inside a <table>
1) Using JQuery.....
$('#tableDivId tr').each(function() {
var tdText = $(this).find("td:first").html();
});
Here you are iterating all rows of a table according to given div id, and then you are fetching first <td> element data for current <tr>....
2) Without JQuery...............
function loadAllTDs(){
var table = document.getElementById('tableDivId');
var rows = table.getElementsByTagName('tr');
var cells, tdText;
for (var i = 0, j = rows.length; i < j; ++i) {
cells = rows[i].getElementsByTagName('td');
if (!cells.length) {
continue;
}
tdText = cells[0].innerHTML; // For 0th td for each tr of this table....
alert(tdText);
}
}
instead of getting each element by getElementById() we can have a single function which assigned to variable $ so we can save our line of code doing like this
ReplyDeletevar $ = function(id) { return document.getElementById(id); };
after this we can just use this as var ele=$('id')