/    Sign up×
Community /Pin to ProfileBookmark

what method to get this function to work onload as well as onclick

Hi,
I have a very simple javascript function which works onclick.

There is an unordered UL list on my page which the function loops through and sets all of the id’s for each LI to null.

It then takes the name of the specific LI that has been click (which is passed in the function variables) and changes it’s ID to “current”

It’s basically so I can show which of the current sections on the page is the current one being used.

Here’s my code:

on a li link:
onclick=”navcontrolchange(this,’innercontrol_projects’);”

the function:

function navcontrolchange(element,innercontrolname,mainnavname){
alert (element)
//grab the info from the top navigation bar nodes
liArray=document.getElementById(“nav”).childNodes;
i=0;
//loop through and set all the id’s to nothing (thus clearing their style settings
while(liArray[i]){
liArray[i].id=””;
i++;
}
//finally change the one we’ve actually clicked on to “current” so this is picked up by the css
element.id=”current”;

//grab the LIinfo from the inner controls list
var menuliArray = document.getElementById(“innercontrols”).getElementsByTagName(“li”);
//loop settings all diplsya to nothing
for (var j=0; j<menuliArray.length; j++) {
menuliArray[j].style.display = “none”;
}
//finally set the one we’ve selected from the top navigation to be visible
document.getElementById(innercontrolname).style.display=”block”;
var innermenuliArray = document.getElementById(innercontrolname).getElementsByTagName(“li”);
for (var k=0; k<innermenuliArray.length; k++) {
innermenuliArray[k].style.display = “block”;
}
}

see? all very simple.

Now I have an additional request… when the user moves a page I need to keep this state so I use an onload behaviour to fire that same function. .however, the onclick behavious used the “this” keyword so it knows which LI was clicked.

When I move pages I am able to pass querystring info so I can keep a track of things. however I cannot see how to pass the “this” value

If I do an alert(this) on the fucntion I just get [object] so that’s no help

Basically I need to alter:
navcontrolchange(this,’innercontrol_<%=MainSection%>’);
to remove the “this” keyword and replace with a hardcoded word/number for the li which has been clicked.

Any ideas?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mtm81authorJun 03.2008 — I've tried that, but it doesn't work for the following reason:

If I set the state on the css, after the javascript function is called as part of the onload.. (i need to call it onload to do some other stuff with the nav..)

that function then resets all of the id's as part of the first loop.. thus resetting any css styling I would have applied..
Copy linkTweet thisAlerts:
@mtm81authorJun 03.2008 — ok sorted it.. cheers fanq
×

Success!

Help @mtm81 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.21,
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,
)...