Forum Moderators: open

Message Too Old, No Replies

How to get <li>TheText</li>?

         

topsport

5:22 pm on Nov 30, 2005 (gmt 0)

10+ Year Member



I am having problems getting "TheText" value!

Is there a way this to be done, without using innerHTML?

Some DOM method?

Fotiman

5:53 pm on Nov 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title>Test</title>
<script type="text/javascript">
function listText()
{
// Get the UL
var ul = document.getElementById("yourUL");
// Get the LI in the UL
var liNodes = ul.getElementsByTagName("li");
// Iterate through the LI's
for( var i = 0; i < liNodes.length; i++ )
{
// Get the child nodes of the LI
var liChildren = liNodes.item(i).childNodes;
// Iterate through the child nodes of the LI
for( var j = 0; j < liChildren.length; j++ )
{
// Look for text nodes
if( liChildren.item(j).nodeType == liChildren.item(j).TEXT_NODE )
{
// Get the text node value
alert( liChildren.item(j).nodeValue );
}
}
}
}

</script>

</head>
<body onload="listText();">

<ul id="yourUL">
<li>Foo</li>
<li>Bar</li>
</ul>

</body>
</html>

SpaceFrog

3:58 pm on Dec 1, 2005 (gmt 0)

10+ Year Member



be careful with childnodes count as IE and firefox do not count the same...