Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOW TO PICK UP DATA FROM AN HTML TABLE CELL/COLUMN

 
Tokunbo Oke
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my Javascript program, I want to pick up the data/value FROM the cell/column of an HTML table and assign it to a variable. Can you please tell how to refer to the data/value of a cell/column of an HTML table? For example in an HTML form, the value of a field can be picked up by specifying:
document.forms[0].firstName.value, how can I pick up the value of a cell in an HTML table?

Thank you.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to use innerHTML is what you can use to grab the text.

If the cell you want is named it is rather easy all you need to do is reference the id


But if you do not have an id to go by, it takes a couple more steps, but it is not that hard as long as you know the number of the row it is in and the number of the cell it is in:


If you have the row named, or you have the row object, you can just do:


Hope that helps you out.

Eric
[edit - fixed a simple typo]
[ January 13, 2005: Message edited by: Eric Pascarello ]
 
Tokunbo Oke
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super!

Your answer is quite exaustive. In a ddition, it is exactly what I asked for.

Thank you very much indeed.

Dele.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ERic
cant we use innerText instead of innerHTML. like document.getElementById("cellid").innerText;
THanks
kundan
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can feel free to us innerText, but you need to realize that innerText is IE only. Mozilla based browsers will get undefined.

Eric
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I blogged today about how to make innerHTML act like innerText

http://radio.javaranch.com/channel/pascarello/2005/01/14/1105721395000.html

Eric
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks Eric
Your blog really helped?? Actually i just forgot the issue of compatibility since my project is IE based.
Thanks again
kundan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic