/    Sign up×
Community /Pin to ProfileBookmark

Passing parameters into a function

I have a button. And in a function, I am changing the onClick attribute.
I have to parameters that I wish to pass into the function from the current function: ‘node’ and ‘datetext’.

var button = document.getElementById(“append”);
button.setAttribute(“onClick”, “SendEditBlog(‘”+ node + “, “+ datetext +”‘)”);

This is how the next function parses the parameters in.

function SendEditBlog(node, datetext)
{
}

When the values parse into the new function, the node variable contains that values of ‘node’ and ‘datetext’, and datetext is undefined.

What have I done??

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@dmboydApr 08.2009 — What are the values of 'node' and 'datetext'? Where are those variables defined? After all, if you're using them, they need to be defined outside of the function. Otherwise, you will get an issue with undefined variables.
Copy linkTweet thisAlerts:
@KorApr 08.2009 — use an anonymous:
<i>
</i>button.onclick=function(){SendEditBlog(node,datetext)}
Copy linkTweet thisAlerts:
@haulinApr 10.2009 — say you have the variables containing this:

var node='foo';

var datetext='bar';

what you were doing resulted in this:

onClick=SendEditBlog('foo,bar') <!-- passing 1 string as an argument -->

if you want to pass their [B]values[/B], do this:
[CODE]button.setAttribute("onClick", "SendEditBlog('"+ node + "', '"+ datetext +"')"); //2 additional apostrophes around the comma[/CODE]
it will result in:

onClick=SendEditBlog('foo','bar') <!-- passing 2 string arguments -->

if you want to pass the [B]variable references[/B], do this:
[CODE]button.setAttribute("onClick", "SendEditBlog(node,datetext)");[/CODE]
which will simply result in:

onClick=SendEditBlog(node,datetext)
Copy linkTweet thisAlerts:
@KorApr 10.2009 — 
[CODE]button.setAttribute("onClick", "SendEditBlog('"+ node + "', '"+ datetext +"')"); //2 additional apostrophes around the comma[/CODE][/QUOTE]

No. IE does not support that. Either you use the crossbrowser classical DOM 0 + anonymous function way (as I said in may previous post), or you use both the Moz and the IE models of attaching the events. See also:

http://www.quirksmode.org/js/events_advanced.html
Copy linkTweet thisAlerts:
@bronni91authorApr 11.2009 — Thanks a lot for everyones input!! =]
×

Success!

Help @bronni91 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.19,
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,
)...