/    Sign up×
Community /Pin to ProfileBookmark

passing var in onclick causes it to fail?

Hello. I’m trying to pass a var to a function using onclick, but when I run it, it plays the function onload, and does not work when I click the link. I’m used to AS3 where you can’t do this, so is javascript the same? If that’s the case, how can I get the object to the onclick function for later use?

This is my handler

[CODE]navLink.onclick = navClick(oSection);[/CODE]

And the function

[CODE]function navClick(S) {
alert(S);
return false;

}[/CODE]

and in context

[CODE]window.onload = initSite;

function initSite() {
var clientsDoc = Sarissa.getDomDocument(“”,””);
clientsDoc.async = false;
clientsDoc.load(“xml/client_data.xml”);

var navItemList = clientsDoc.getElementsByTagName(“navItem”);
//make main nav
for(i=0; i<navItemList.length; i++) {
var navLink = document.getElementById(“nav_”+(i+1));
//build XML object
var oSection = new Object();
oSection.linkName = navItemList[i].getAttribute(“name”);
var linkText = document.createTextNode(oSection.linkName);
navLink.appendChild(linkText);
navLink.onmouseover = navOver;
navLink.onmouseout = navOut;
navLink.onclick = navClick(oSection);
}
var links = document.getElementById(“nav_”+(i+1));
}

function navOver() {
this.parentNode.className = “active-link”;

}

function navOut() {
this.parentNode.className = “inactive-link”;
}

function navClick(S) {
alert(“”);
return false;

}[/CODE]

Thanks for any help you can offer.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooApr 27.2009 — Make the handler a reference to a function, and not a call to the function-

In javascript, parentheses that follow a function identifier define an operator-

the function expression runs immediately.

[CODE]
// make sure navLink and oSection and navClick are defined.

navLink.onclick= function(){
return navClick(oSection);
}
[/CODE]
Copy linkTweet thisAlerts:
@cameronmcefeeauthorApr 27.2009 — Oh, that makes sense. I'll have a go at that.
Copy linkTweet thisAlerts:
@cameronmcefeeauthorApr 28.2009 — Alright, I understand what's going on there, and what you've posted works.

The problem now is that if I pass it:
[CODE]navLink.onclick= function(){
return navClick(oSection.linkName);
}[/CODE]

[CODE]function navClick(S) {
alert(S);
return false;
}
[/CODE]

Each link returns the same value. The way this is set up (or at least what I am trying to do), is to give each link an oSection object. This should result in the above code returning a different value for each link.
×

Success!

Help @cameronmcefee 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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