By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Read text in <li>

P: n/a
If I have:
<ul id="theUL">
<li id="anLi1">some text</li>
<li id="anLi2">some other text</li>
<ul>

How can I get the value of the text within the the li's?
Any help much appreciated.
thanks
Charlie.

Oct 1 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
wrote on 01 okt 2005 in comp.lang.javascript:
If I have:
<ul id="theUL">
<li id="anLi1">some text</li>
<li id="anLi2">some other text</li>
<ul>

How can I get the value of the text within the the li's?


Try:

alert(document.getElementById('theUL').firstChild. innerHTML)
however the li id-s do not work in IE6
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 1 '05 #2

P: n/a
Thanks for this, allthough I'm not sure I was 100% clear with my
question, I want to reference a specific <li>'s by ID and get its
text.

I'm not that bothered about older browsers compatibility but IE6 and
firefox are a must.

Oct 1 '05 #3

P: n/a
> "cb*******@gmail.com" <cb*******@gmail.com> wrote:
news:11**********************@g49g2000cwa.googlegr oups.com....

If I have:
<ul id="theUL">
<li id="anLi1">some text</li>
<li id="anLi2">some other text</li>
<ul> ^ should be a closing tag </ul>
How can I get the value of the text within the the li's?
Any help much appreciated.
thanks
Charlie.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<ul id="theUL">
<li id="anLi1">some text</li>
<li id="anLi2">some other text</li>
</ul>
<button onclick=
"alert(document.getElementById('anLi1').childN odes[0].data)">anLi1</button>
<br>
<button onclick=
"alert(document.getElementById('anLi2').childN odes[0].data)">anLi2</button>
</body>
</html>

--
BootNic Saturday, October 01, 2005 5:04 PM

A man's got to do what a man's got to do. A woman must do what he can't.
*Rhonda Hansome*

Oct 1 '05 #4

P: n/a
Perfect!
thanks
Charlie.

PS. that closing ul tag was just a typo in the post, but well spotted.

Oct 1 '05 #5

P: n/a
gibster wrote:
Perfect!
thanks
Charlie.

PS. that closing ul tag was just a typo in the post, but well spotted.


You may want to also considert using textContent for DOM 3 compliant
browsers (Mozilla et al and maybe IE one day) and innerText for IE.
That way your li can contain other markup that will be automatically
removed.

<script type="text/javascript">
function showContent( id )
{
var el = document.getElementById(id);

if ( el.textContent ){
alert(el.textContent);
} else if ( el.innerText ){
alert(el.innerText);
}
}
</script>

<ul id="theUL">
<li id="anLi1"><b>some</b> <i>text</i></li>
<li id="anLi2"><span style="color:red;">some</span> other text</li>
</ul>
<button onclick="showContent('anLi1')">anLi1</button>
<br>
<button onclick="showContent('anLi2')">anLi2</button>

--
Rob
Oct 2 '05 #6

P: n/a
Thanks Rob,
I'll keep that in mind in case I need to add extra markup in the Li's
but for the time being I'll stick with BootNic's suggestion.
Cheers
Charlie.

Oct 2 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.