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);    
    }
}

Comments

  1. Ashok kumar Knowserve Student java BatchDecember 27, 2011 at 1:35 AM

    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
    var $ = function(id) { return document.getElementById(id); };
    after this we can just use this as var ele=$('id')

    ReplyDelete

Post a Comment

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

How to create mail message in FRC822 format