/    Sign up×
Community /Pin to ProfileBookmark

.nodeValue JavaScript issue

I’m just starting to learn JavaScript and DOM and I have a newbie question for you.
I’m having some problems with the nodeValue method, so I would like if someone could explain me what am I doing wrong here.
Here’s my code:

[CODE]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Untitled Document</title>
<script type=”text/javascript” src=”test.js”></script>
<style>
@import “stil.css”;
</style>
</head>
<body>

<ul id=”glavno” >
ITEMS
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script type=”text/javascript”>
l1=document.getElementById(“glavno”);
for (i=0;i<l1.childNodes.length;i++){
if(l1.childNodes[i].nodeName==”#text”){
document.write(l1.childNodes[i].nodeValue);
}
}

</script>
</body>
</html>
[/CODE]

If you test the code you’ll see that document.write writes only “ITEMS”, and I’m wondering why is that so? Shouldn’t it write values between <li> tags since li elements are child elements/nodes?
I just want to show the content that is between <li> tags, so should I use innerHTML instead?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TcobbJan 05.2011 — element.childNodes is a nodelist that contains all the direct children of element. The list does not contain its "grandchildren," which in your case is what you want--the text nodes that are children of the LI elements.

Try this:

[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="test.js"></script>
<style>
@import "stil.css";
</style>
</head>
<body>

<ul id="glavno" >
ITEMS
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<script type="text/javascript">
var i, j;
var l1=document.getElementById("glavno"); // get the reference for the container
var items = l1.childNodes; //stick the nodelist into another variable for convenience
for (i = 0; i < items.length; i ++){ //loop through all the childNodes of GLAVNO
if(items[i].tagName == 'LI'){ //if its an LI element
alert(items[i].firstChild.data); //access the text of the LI's child textnode
}
}

</script>
</body>
</html>
[/CODE]


WARNING: If you have a more complex HTML structure this simple script won't work (or at least the probability is high)
×

Success!

Help @Mentalhead 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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