/    Sign up×
Community /Pin to ProfileBookmark

How to obtain and change nodeValue text?

Trying to understand how to obtain the text of a td tag element within a table and especially how to change that text using xml DOM and javascript. here’s what I have so far; any help appreciated…

[CODE]<!DOCTYPE html PUBLIC “”
<html xmlns=””>

<head>
</head>

<body>
<table border=”1″>
<tr>
<td>some text here</td>
</tr>
</table>
</body>

<script type=”text/javascript”>
<!– Try several ways to write out text of the td tag ??? –>
document.write(document.getElementsByTagName(“td”).item[0]);
document.write(document.getElementsByTagName(“td”).item[0].childnodes[0].nodeValue);
var TDElement0 = document.getElementsByTagName(“td”).item[0];
document.write(TDElement0);

<!– Try to change the text of the td tag ??? –>
document.getElementsByTagName(“td”).item[0].childnodes[0].nodeValue = “different text here”;

</script>

</html>[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 29.2007 — Use after the document has loadeddocument.getElementsByTagName("td")[0].firstChild.data = "different text here";
Copy linkTweet thisAlerts:
@paul_116authorSep 29.2007 — Yes, thanks! Not waiting for the page to load was the problem however I want the user to click on the "some text here" text and an event handler then causes the text to be changed via getElementsByTagName("td")[0].firstChild.data

The way it stands (see new code below) the text changes to "different text here" before the user sees the original "some text here"

[CODE]<html>

<head>
<script type="text/javascript">

function load()
{
document.getElementsByTagName("td")[0].firstChild.data = "different text here";
}

</script>

</head>

<body onload="load()">
<table border="1">
<tr>
<td>some text here</td>
</tr>
</table>
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@FangSep 29.2007 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Basic HTML&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
window.onload=function() {
var o = document.getElementsByTagName("td")[0];
o.onclick=function(){this.firstChild.data = "different text here";};
};
&lt;/script&gt;

&lt;style type="text/css"&gt;

&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;table border="1" cellpadding="0" cellspacing="0" summary=""&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;some text here&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@paul_116authorSep 29.2007 — Excellent! Thanks very much...
×

Success!

Help @paul_116 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...