Help - Search - Members - Calendar
Full Version: using charAt() on a collection of tags
HTMLHelp Forums > Programming > Client-side Scripting
ctoz
I have a whole bunch of <divs>, each containing six <p> tags; these have a class, "L _n", assigned, where "n" is a number from 1 to 6:
CODE

<div>
<p class="L _6">___</p>
<p class="L _5">_ _</p>
<p class="L _4">___</p>
<p class="L _3">_ _</p>
<p class="L _2">___</p>
<p class="L _1">_ _</p>
</div>
(think I Ching).

The content of every p with class "L" is always either "---" or "- -".

I'm trying to get my head around switching the visibility of the parent <div> by accessing the state of the second character in a child <p> tag, which will be "-" or " ", by using charAt(1).

First problem is how to access this character in each of a collection of <p> tags. Possibly something like:

var str="-";
get <p> elements by TagName;
collect all <p>s with certain class;
for these <p>s, if charAt(1) = str, hideparent()...

I can possibly write it, once I get the logic right. Any suggestions welcome.
Christian J
QUOTE(ctoz @ Aug 23 2009, 09:12 AM) *

First problem is how to access this character in each of a collection of <p> tags. Possibly something like:

var str="-";
get <p> elements by TagName;
collect all <p>s with certain class;
for these <p>s, if charAt(1) = str, hideparent()...

That seems right. One issue is that the className will return both classes "L" and "_n", to separate them you might use the split() method on the spaces in the className. Use innerHTML or firstChild.nodeValue to get the content of the P. To access the parent DIV, use the parentNode property.

Another approach might be to loop through the DIVs and examine e.g. the third P element in each, that way you don't need the classes.
ctoz
Thanks.
QUOTE
Use innerHTML or firstChild.nodeValue to get the content of the P

New territory for this cut'n'paster.I may be back fairly soon.

Cheers
Christian J
QUOTE(Christian J @ Aug 23 2009, 12:51 PM) *

Use innerHTML or firstChild.nodeValue to get the content of the P.

This assumes the P contains only plain text, though. If there are elements inside the P it doesn't work as expected.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.