0

I just want to get the cell values from the rows of a column, but I do no want to get the undefined at the start. I think this undefined is the is the undefined td in the first tr of the table.

How do I just get the cell values of the rows from a column (and not the header) of the table?

my current fiddle gets me this in the the console:

undefined
123
456
789

or

undefined
abc
def
ghi

so the answer I would expect is

123
456
789

or

abc
def
ghi

Note Similar question here which has helped get to this point

Community
  • 1
  • 1
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98

5 Answers5

4

You can exclude the first header row with #mytable tr:not(:first) selector:

$('#mytable tr:not(:first)').each(function() {
    var customerId = $(this).find("td").eq(0).html();
    console.log(customerId)
});
dfsq
  • 191,768
  • 25
  • 236
  • 258
1

Simple check would solve it:

$('#mytable tr').each(function() {
  if ($(this).find("td").eq(0).length) {
    var customerId = $(this).find("td").eq(0).html();
    console.log(customerId)
  }
});

Fiddle: https://jsfiddle.net/praveenscience/cLv9gk08/

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

Here you go: Fiddle

$('#mytable tr').each(function() {
    var customerId = $(this).find("td:first").html();    
});


//OPTION 2 
//To select a particular cell, you can reference them with an index:
$('#mytable tr').each(function(index) {
if(index !== 0){
   var customerId = $(this).find("td").eq(0).html();    
    console.log(customerId);
}

});
itamar
  • 3,837
  • 5
  • 35
  • 60
1

It would be better to put the header row into a <thead> section and all the rows into a <tbody> so you can then use $('#mytable tbody tr').each(.....); which will automatically exclude the header.

Steve Harris
  • 5,014
  • 1
  • 10
  • 25
-1

var customerId = $(this).find("td").html();

jacobcc
  • 316
  • 3
  • 10