Passing Variables not working
Hi, this is probably quite a basic one, but I'm struggling to get an argument passed in to a function successfully.
When I do this, the code doesn't execute:
Code:
document.onclick = newTarg(5);
document.ondblclick = newTarg(10);
function newTarg(spd) {
alert('Working = '+spd);
// More code here
}
Doing this instead works fine:
Code:
document.onclick = newTarg;
function newTarg() {
alert('Working');
// More code here
}
Any ideas why it won't let me pass a variable in? I've never really formally learnt Javascript so this is getting a bit frustrating...
document.onclick = function(){newTarg(5);}
document.ondblclick = function(){newTarg(10);}
use [code]YOUR CODE GOES HERE [/code] or burn in Hell
use [code]YOUR CODE GOES HERE [/code] or burn in Hell
Just to help me learn(!), why did that work?
Seems weird that it happily called newTarg, but only until I needed to pass a value in to it...
my english is not good enough for explaining, just take a look at this article (DOM element properties, DOM Event interface) or try to find smth alike to read in english
use [code]YOUR CODE GOES HERE [/code] or burn in Hell
With a document.onclick you affect a function, not a function call, on the onclick event !
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks