/    Sign up×
Community /Pin to ProfileBookmark

manipulating childnodes unobtrusively

i’m just starting to scratch the surface of unobtrusive JS so bare with me…

I have a parent <div> with an id of “content” that contains everything i want to manipulate. Within that <div> there are going to be 1 or more <div>s separating blocks of content (see code). Here’s the deal…

1) I want to capture an array of the “block” <div>s that are one level below the “content” div.
2) I want to capture the array of <div>s that are one level below each “block” div.

I’m assuming if i use the following it will capture ALL <div>s below the “content” not just those one level deep…right?

[code]var blocks=document.getElementById(‘content’).getElementsByTagName(‘div’);[/code]

i’ve been playing with using childnode calls, but i’m not grasping it i guess. i also have to deal with other possible content besides the divs. Any suggestions?

…and here’s the markup…

[code]<div id=”content”>

<div><!– BEGIN BLOCK 1 –>
<img src=”xxx” />
<div>
stuff here…
</div>
<div>
stuff here…
</div>
<div>
stuff here…
</div>
</div><!– END BLOCK 1 –>

<div><!– BEGIN BLOCK 2 –>
<img src=”xxx” />
<div>
stuff here…
</div>
<div>
stuff here…
</div>
<div>
stuff here…
</div>
</div><!– END BLOCK 2 –>

</div>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 09.2006 — a parent has childNodes. (this is what you have called "one level below the parent"). The childNodes might have different nodeType and nodeName - that means they might be "tagged" childNodes (elements), text (textNodes), comments.... and if they are tags, they might have differenmt tag names (nodeName) - DIV, SPAN...

So that, first you must build the childNodes collection

var kids=[I]root[/I].childNodes;

Then you have to circle through the childNodes and to build a new "sub-collection" of the childNodes which are DIVs (nodeName=='DIV')

var i=0, k=[], allK

while(allK=kids[i++]){

allK.nodeName=='DIV'?k[k.length]=allK:null;

}

Now here's an example:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function returnKids(root){
var kids=root.childNodes;
var i=0, k=[], allK
while(allK=kids[i++]){
allK.nodeName=='DIV'?k[k.length]=allK:null;
}
return k
}
onload=function(){
var firstLevelDivs=returnKids(document.getElementById('content'));
alert(firstLevelDivs[0].innerHTML);
alert(firstLevelDivs[1].innerHTML)
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div id="content"&gt;

&lt;div&gt;
&lt;div&gt;block1 div1&lt;/div&gt;
&lt;div&gt;block1 div 2&lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;
&lt;div&gt;block2 div 1&lt;/div&gt;
&lt;div&gt;block2 div 2&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@bcamp1973authorOct 11.2006 — exactly what i was looking for, thank you! ?
×

Success!

Help @bcamp1973 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...