473,800 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read text in <li>

If I have:
<ul id="theUL">
<li id="anLi1">som e text</li>
<li id="anLi2">som e 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
6 2281
wrote on 01 okt 2005 in comp.lang.javas cript:
If I have:
<ul id="theUL">
<li id="anLi1">som e text</li>
<li id="anLi2">som e other text</li>
<ul>

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


Try:

alert(document. getElementById( 'theUL').firstC hild.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
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
> "cb*******@gmai l.com" <cb*******@gmai l.com> wrote:
news:11******** **************@ g49g2000cwa.goo glegroups.com.. ..

If I have:
<ul id="theUL">
<li id="anLi1">som e text</li>
<li id="anLi2">som e 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">som e text</li>
<li id="anLi2">som e other text</li>
</ul>
<button onclick=
"alert(document .getElementById ('anLi1').child Nodes[0].data)">anLi1</button>
<br>
<button onclick=
"alert(document .getElementById ('anLi2').child Nodes[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
Perfect!
thanks
Charlie.

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

Oct 1 '05 #5
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.getEle mentById(id);

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

<ul id="theUL">
<li id="anLi1"><b>s ome</b> <i>text</i></li>
<li id="anLi2"><spa n style="color:re d;">some</span> other text</li>
</ul>
<button onclick="showCo ntent('anLi1')" >anLi1</button>
<br>
<button onclick="showCo ntent('anLi2')" >anLi2</button>

--
Rob
Oct 2 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
6892
by: Michael | last post by:
This is a two-part question to which I haven't been able to find an answer anywhere else. 1. Is it possible to format the bullet/number character of the <li>? In my styles sheet, I have the <li> tag formatted, for example, bold. However, when it's applied, the number of the <li> is not bold, but the text is. Do I have to apply the bold to the <ol> instead? 2. When I use <li>example text</li>, and when I insert a <br> after the </li>,...
1
3760
by: Randall Sell | last post by:
OK, I am utterly stumped. The code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style type="text/css"> ul { background-color: red; }
3
10776
by: abro | last post by:
Problem: A list contained in a div contains several items that are made of two parts: itemName and itemValue. ie: <div id="data"> <li>longtime1 <span> 1326 mins></span></li> <li>longtime2 <span> 1692 mins</span></li> <li> time1 <span> 24 mins </span></li>
4
2210
by: abs | last post by:
Anybody has an idea how to get the <ul> element which is not nested in <li> element ? In other words I have several lists like this: <ul id="1"> <li>Aaaaaaaa</li> <li>Bbbbbbbb</li> <li>Cccccccc <ul> <li>111111</li> <li>222222</li>
2
7634
by: Shaun | last post by:
Hello! I have a quick question regarding CSS and having it applied to all elements. I am trying to eliminate the gap between a paragraph and a list that usually occurs in html and I've found away to do that with this code: <p>a</p> <ul style="margin-top: -20; padding-top: 0"> <li>1</li>
2
2573
by: Andrew Donaldson | last post by:
I'd welcome some help in understanding what's going on with graphical browsers in the navigation list at: http://www.bounceandtickle.org.uk/index.html (This site is not about what it might seem from the URL!) I'm using a <ul> for the gimmicky teddy bear links. The teddy is a background image for each <li>. Each link 'button' is an <a> within an <li>, and I'm trying to get the button to appear partway down the teddy
7
1872
by: gener672 | last post by:
Problem: font-size seems to be rendered differently for <li class="issue"> and for <p class="issue">. (Example is in: www.dvmvac.com/snippet/ ... ignore background - I just did a quick file copy and didn't get bkgnd file resolved.) In external CSS, I define 'body' to have a font-size of 10px .... and 'h2' to be 300%(which works - see header) .... then I define (or redefine) 'p' to be 120%. Inline (testing there before exporting to css...
9
3482
by: eros | last post by:
<li>List <ul> <li>List1</li> <li>List2</li> <li>List3</li> </ul> </li> How to call the php code from that html tags... when I click List1?
2
7069
by: GloStix | last post by:
For some reason, FF likes to put a black underline on all my buttons. No matter what I do, it has the line I've tried displaying as block and cursor, anything.. Also I've been trying to get it so the entire block is clickable, not just the text. I've sorta got it so it's clickable, just some parts of the button I still can't click on. The website is http://pdhtdev.johnabbott.qc.ca/students/2010/james_mann/portfolio/index.htm The css...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.