Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Looping through a list

Reply
Thread Tools

Looping through a list

 
 
simon_s_li@hotmail.com
Guest
Posts: n/a
 
      10-10-2005
Hi,

I have a list (i..e <UL> <LI>) where each <LI> contains a <DIV> and
text in it.

Example:

<UL>
<LI>
<DIV> text here 1</DIV>
</LI>
<LI>
<DIV> text here 2</DIV>
</LI>
<LI>
<DIV> text here 3</DIV>
</LI>
<UL>

Using javascript how do I read the content of each item in the list.

Regards
Simon

 
Reply With Quote
 
 
 
 
Baconbutty
Guest
Posts: n/a
 
      10-10-2005
1. You will need to get a reference to the UL element. You could give
it an "id" and use document.getElementById.

2. You will need to teach yourself about the DOM (document object
model) for HTML. The object returned by "getElementById" will be an
Element object, which exposes DOM properties and methods, e.g.
"childNodes", "firstChild", "data" etc. Use these to find the Text
nodes of the DIV elements, and read their content.

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      10-10-2005
wrote on 10 okt 2005 in comp.lang.javascript:

> I have a list (i..e <UL> <LI>) where each <LI> contains a <DIV> and
> text in it.
>
> Example:
>
> <UL>
> <LI>
> <DIV> text here 1</DIV>
> </LI>
> <LI>
> <DIV> text here 2</DIV>
> </LI>
> <LI>
> <DIV> text here 3</DIV>
> </LI>
> <UL>


</UL> !!!!

>
> Using javascript how do I read the content of each item in the list.


Many ways, here an example:

<UL id=ulx>
<LI>
<DIV> text here 1</DIV>
</LI>
<LI>
<DIV> text here 2</DIV>
</LI>
<LI>
<DIV> text here 3</DIV>
</LI>
</UL>

<script type='text/javascript'>

var ulx = document.getElementById('ulx')

f = ulx.firstChild
t = f.innerHTML
alert(t)
t = f.firstChild.innerHTML
alert(t)

f = f.nextSibling
t = f.innerHTML
alert(t)
t = f.firstChild.innerHTML
alert(t)

f = f.nextSibling
t = f.innerHTML
alert(t)
t = f.firstChild.innerHTML
alert(t)

</script>




--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
Robi
Guest
Posts: n/a
 
      10-10-2005
Evertjan. wrote in message news:Xns96EBA9FA231B0eejj99@194.109.133.242...
> wrote on 10 okt 2005 in comp.lang.javascript:
>
>> I have a list (i..e <UL> <LI>) where each <LI> contains a <DIV> and
>> text in it.

[...]
>> Using javascript how do I read the content of each item in the list.

>
> Many ways, here an example:
>
> <UL id=ulx>
> <LI>
> <DIV> text here 1</DIV>
> </LI>
> <LI>
> <DIV> text here 2</DIV>
> </LI>
> <LI>
> <DIV> text here 3</DIV>
> </LI>
> </UL>
>
> <script type='text/javascript'>
>
> var ulx = document.getElementById('ulx')
>
> f = ulx.firstChild
> t = f.innerHTML
> alert(t)
> t = f.firstChild.innerHTML
> alert(t)
>
> f = f.nextSibling
> t = f.innerHTML
> alert(t)
> t = f.firstChild.innerHTML
> alert(t)
>
> f = f.nextSibling
> t = f.innerHTML
> alert(t)
> t = f.firstChild.innerHTML
> alert(t)
>
> </script>


different browsers tend to react differently to white spaces between tags
<ul>
<li>
<div>text</div>
</li>
</ul>
in Firefox for instance, will end up as
--+-- <ul> ELEMENT_NODE
| +-- TEXT_NODE
| +-+-- <li> ELEMENT_NODE
| | +-- TEXT_NODE
| | +-+-- <div>- ELEMENT_NODE
| | | --- "text" TEXT_NODE
| | --- TEXT_NODE
| --- TEXT_NODE
--- TEXT_NODE

Why do I mention this?
because you need to find the nodes that get to the text you want.

Evertjan showed you a simple way to access the text.
although if you don't write
<ul><li><div>text 1</div></li><li><div>text 2</div></li></ul>
you're going to end up with errors in Firefox.

you can access the UL or if you have more than one UL
with getElementsByTagName()

I modified Evertjan's script a bit to /loop/ through all ULs
and the LIs.
What I did not do is loop through nested lists.
(other than that they would just add up to the number of ULs)

I'm afraid there are other issues which hopefully will be pointed out
by someone who knows more about nuances and pitfalls of JavaScript

<script type="text/javascript">
var uls = document.getElementsByTagName("ul");
alert("how many ULs? " + uls.length);
var ulx, l, c, t;

for (var ulxs=0; ulxs<uls.length; ulxs++){
ulx = uls[ulxs];
for (var lis=0; lis<ulx.childNodes.length; lis++){
l=ulx.childNodes[lis];
if (l.nodeType==1) {
c=l.innerHTML;
if (!!l.textContent) t=l.textContent;
else if (!!l.firstChild.innerHTML) t=l.firstChild.innerHTML;
alert("the content is:\n" + c + "\nor as text\n" + t)
}
}
}
</script>

The above script would probably be better if implemented as a function()
and then called when needed.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      10-10-2005
Robi wrote on 10 okt 2005 in comp.lang.javascript:

> Evertjan showed you a simple way to access the text.
> although if you don't write
> <ul><li><div>text 1</div></li><li><div>text 2</div></li></ul>
> you're going to end up with errors in Firefox.
>


That is why it seems far better to give all the <div>s a seperate id.

<ul><li>
<div id='t1'>text 1</div></li>
<li>
<div id='t2'>text 2</div></li></ul>

These can be easily be looped through [without evil eval()]:

for (i=1;i<3;i++)
alert(document.getElementById('t'+i).innerHTML)


--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
std::list: remove from front without deleting or looping through whole list Andy C++ 3 06-08-2007 09:02 PM
Re: looping through a list of lists. Rob Hunter Python 2 10-08-2003 05:31 PM
looping through a list of lists. saoirse_79 Python 0 10-08-2003 03:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57