SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
-
Nov 21, 2007, 23:39 #1
- Join Date
- Jul 2005
- Location
- New Orleans, LA (Picayune, MS)
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cannot assign a function to JavaScript event; o_O.
Alright, well I am trying to assign a function to a JavaScript Event for a retrieved element.
I will explain with code first :
Code:window.onload = function() { var divBox = document.getElementById(someid) var divBox.onclick = someOtherFunction(arg,arg) } function someOtherFunction(arga,argb){ var anotherDiv = document.getElementById(someid) anotherDiv.style.color='blue' }
Any suggestions ? Thanks a lot guys
-
Nov 22, 2007, 00:26 #2
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What you need to assign to the .onclick property is a function (a function reference).
What you are doing is calling a function and assigning the return value from it. Unless someOtherFunction(arg,arg) returns a function reference, it won't work.
Here's one way to do it:
Code JavaScript:divBox.onclick = function() { someOtherFunction(arg,arg); };
Here's another:
Code JavaScript:divBox.onclick = aThirdFunction; function aThirdFunction() { someOtherFunction(arg,arg); };
Code:var divBox.onclick = aThirdFunction();
Last edited by AutisticCuckoo; Nov 22, 2007 at 06:00. Reason: Errors fixed
Birnam wood is come to Dunsinane
-
Nov 22, 2007, 02:10 #3
- Join Date
- Jul 2005
- Location
- New Orleans, LA (Picayune, MS)
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hmm, I was going to try that but I didn't, duh... thanks a LOT.
Got it all working now.
cheers
-
Nov 22, 2007, 03:28 #4
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Oh, and the var keyword is for declaring a variable. You don't use it, when assigning to a property of an object. So that should be:
Code:divBox.onclick = function() { someOtherFunction(arg,arg); };
Code:var divBox.onclick = function() { someOtherFunction(arg,arg); };
-
Nov 22, 2007, 06:01 #5
- Join Date
- Nov 2004
- Location
- Ankh-Morpork
- Posts
- 12,158
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 22, 2007, 07:06 #6
- Join Date
- Mar 2004
- Location
- Kenneth City, FL
- Posts
- 823
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Man I was looking at this thread thinking I know damn well I replied to it earlier today. Then I realized I replied to it on another forum.
-
Nov 22, 2007, 11:31 #7
- Join Date
- Jul 2005
- Location
- New Orleans, LA (Picayune, MS)
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Lool Joe...
thanks kyber and cuckoo, yeah I didn't even notice I did that in the example code I put up, just had that problem in the example
Thanks again guys!
Bookmarks