Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the cell value of an HTML table

Status
Not open for further replies.

MarvinManuel

Programmer
Apr 28, 2005
11
PH
Hello,
I want to ask on how to get the value of a cell in an HTML table using javascript.

I have this code but is not working:
var Table1 = document.getElementById('tblGrade');
s = Table1.rows[0].cells[0];

thanks in advance...
 
Try either:

Code:
s = Table1.rows[0].cells[0].firstChild.nodeValue;

or non-standards compliant, but well catered for:

Code:
s = Table1.rows[0].cells[0].innerHTML;

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks for your response.

I've already tried before the firstchild.nodeValue and that didnt work.
Now I used this .innerHTML, it is working but it also captures the tags i used prior to the cell value.
For example, if rows[0].cells[0] = "AAA",
it displays --- <font face="arial" size=2> AAA </font>

Am I missing something?
 
.innerText is NOT cross-browser. .firstChild.nodeValue is.

I suspect it did not work as you typed "child" with a lowercase "c", not uppercase as I showed you.

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I followed the proper casing for firstChild.nodeValue but I get "null" value.
Anyway I found a way to eliminate my tags using CSS so that I can use .innerHTML.
.innerText on the other hand also works for my browser.

Thank you so much guys!
 
where is it stated that .innerText is NOT crossbrowser? i can swear ive used and tested it in three browsers ie,ff,netscape 7. all this time i had neva known it was not cross browser.

---------------------------
ServerOp: LogicSoft
 
where is it stated that .innerText is NOT crossbrowser?

I don't know. I'm speaking from experience. Take the following:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function myFunc() {
			alert(document.getElementById('myDiv').innerText);
			alert(document.getElementById('myDiv').firstChild.nodeValue);
		}
	//-->
	</script>
</head>
<body onload="myFunc();">
	<div id="myDiv">Some text</div>
</body>
</html>

The first alert gives "undefined" in NN and FF, while the second does not.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
I followed the proper casing for firstChild.nodeValue but I get "null" value.

Then the first child has a value of null. Maybe it is a text node with whitespace? try deleting all whitespace between the open tag and the content.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top