1

I need to read all the values of the first column of an HTML table, and I don't want to use DOM methods. I'd like a simpler solution using Prototype or YUI.

Note: the table is generated by a grid widget provided by an external team. That's why we cannot be sure of which IDs or Classnames are used.

In short I'd like something like:

For each row in table XXX
   value = row.column[1].value
   do something with value ...
End For

Thanks in advance

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • what does the gride widget's code which generates the table look like? or what does the generated table look like? – jedierikb Feb 18 '09 at 22:53

4 Answers4

2

You should be able to do something like $$('tr td:first-child') which should get the first td from each tr. You should then be able to use .innerHTML to get the value.

Cozzman
  • 86
  • 3
  • 1
    You could also write : $$('tr td:nth-child(1)'). Usefull if you want to select an other row than the first one. As from prototype 1.5.1 any CSS3 selector should work http://www.w3.org/TR/2001/CR-css3-selectors-20011113/#selectors – kevin Mar 05 '10 at 14:41
1

possibly $$('tr') might work. It returns an array of elements based on a CSS rule.

Mike
  • 3,219
  • 23
  • 29
1

Using Prototype

$('thetable').childElements()[0].childElements().invoke('firstDescendant');

It all cascades. The first one $('thetable') gets the table. I know you wanted minimal DOM but obviously you'll need to identify the table.

The second one gets all the childElements as an array and grabs the first one which is a tbody item.

Next it grabs of those elements' children which provides an array of table rows.

The last one calls invoke() calls a function on each element in that array. In this case, firstDescendant.

Paulo
  • 4,275
  • 2
  • 20
  • 20
0

this will get you an array of all the text values in the first column

var arr = $$('tr:first-child').pluck('innerText');

pluck

$$

Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58