/    Sign up×
Community /Pin to ProfileBookmark

Need help with javascript/css tree

I’m trying to build a tree with javascript, but I’m making some mistake in my code, and am not able to identify it.

Here is my javascript code:

[CODE]function tree_init(){

varTree = document.getElementById(“tree”)
varState = varTree.getElementsByTagName(“li”);

for(i=0;i<varState.length;i++){
varCity = varState.item(i).getElementsByTagName(“ul”);
if(varCity.length > 0) {
varCity.item(0).style.display = “none”;
varState.item(i).onclick = function() {
if(this.getElementsByTagName(“ul”).item(0).style.display == “none”){
this.getElementsByTagName(“ul”).item(0).style.display = “block”;
this.style.listStyleImage = “url(‘../images/tree_open.jpg’)”;

}
else
{
this.getElementsByTagName(“ul”).item(0).style.display = “none”;
this.style.listStyleImage = “url(‘../images/tree_close.jpg’)”;

}

}
}
}
}[/CODE]

If I expand the top level, it expands. But when I click on a second level, it closes the parent, whereas it should expand the second level. (It does expand the expand the 2nd level, but we can’t see it becoz it closes the top level).

Thank you, in advance

[QUOTE][/QUOTE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toenailsinMay 24.2008 — you'd need to attach the onclick function to an object in the list instead of the whole list object.
×

Success!

Help @vpraveen84 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 6.1,
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,
)...