/    Sign up×
Community /Pin to ProfileBookmark

Recursive scope problems?

Given the HTML:

[code=html]
<div id=”testing”>
<p class=”clear”></p>
<p class=”d”>
<b>123</b>
</p>
<p class=”e”></p>
<p class=”f”></p>
</div>[/code]

And given JS:

[code=php]
family = [];
nodeRec = function(node){
var i;
if(node.hasChildNodes()){
temp = node.childNodes;
for(i=0; i<temp.length; i++){
if(temp[i].nodeType == 1)
family.push(nodeRec(temp[i]));
}
}
return node;
}
[/code]

I am getting this output:

[code=php]
>>>
family
[p.clear, b, p.d]
[/code]

Maybe this is because I haven’t had my morning coffee yet, but is this a scope problem, and how do I go about fixing my code.

Help appreciated,

Dmitri.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Apr 28.2008 — Is your question about the ordering of the array? It's because p.d "hasChildNodes()", so it is running the child nodes through the function first, pushing them onto the array, then continuing and pushing p.d onto the array. I'm not sure how to "fix it", because I'm not entirely sure what your going for here.
Copy linkTweet thisAlerts:
@ZeroKilledApr 28.2008 — i guess i understood his/her problem. he/she want to collect node of type element within a specified element. problem is that it is not collecting every node due to variable scope. the variable [B]temp[/B] is global, hence each call to [B]nodeRec[/B] replace the content with a newer one. prefix the variable name with the statement [B]var[/B]:

...
var temp = node.childNodes;
...
Copy linkTweet thisAlerts:
@KorApr 28.2008 — On the other hand, if you want to get all the first level "tag" childNodes of a [I]parent[/I] along with all the second level "tag" childNodes of the first level "tag" childNodes and so on till the last branch level of this tree, why not simply use a wild card?:
<i>
</i>var family=[I]parent[/I].getElementsByTagName('*');

For instance, in your example:
<i>
</i>var family=document.getElementById('testing').getElementsByTagName('*');

will return the collection of all the "tag" elements inside the parent div 'testing', down to the last branch:

p,p,b,p,p
Copy linkTweet thisAlerts:
@TJ111Apr 28.2008 — Ahh somehow I missed the fact that elements were missing |>_<|
Copy linkTweet thisAlerts:
@DmitriFauthorApr 28.2008 — Thanks everyone, yep fixed it by redefining temp each time.

Kor: this allows more flexibility including if I want to search for text nodes later. The reason I am trying to maintain full control, is I am currently writing a CSS parser extension to my library so I want to oversee everything. So far almost done level 2 specs, moving onto the third.
×

Success!

Help @DmitriF 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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