SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member vilStewart's Avatar
    Join Date
    Jul 2005
    Location
    New Orleans, LA (Picayune, MS)
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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'
    }
    If I attempt to do something similar to the above code, it executes 'someOtherFunction' rather than just setting the onclick event to the function with the proper arguments that I need to send it.

    Any suggestions ? Thanks a lot guys
    stewart::howe
    Web Developer & Programmer
    CelerMedia.Com | iAmStewart.com | CelerLabs.com

  2. #2
    SitePoint Author silver trophybronze trophy

    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);
    };
    Note that there must be no parentheses after the function name in the assignment. This is wrong:
    Code:
    var divBox.onclick = aThirdFunction();
    Last edited by AutisticCuckoo; Nov 22, 2007 at 05:00. Reason: Errors fixed
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Member vilStewart's Avatar
    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
    stewart::howe
    Web Developer & Programmer
    CelerMedia.Com | iAmStewart.com | CelerLabs.com

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    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);
    };
    Instead of:
    Code:
    var divBox.onclick = function() {
      someOtherFunction(arg,arg);
    };

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Oh, and the var keyword is for declaring a variable.
    Duh! I didn't even twig to that error, because I was focusing solely on the function reference/call business.
    Birnam wood is come to Dunsinane

  6. #6
    Floridiot joebert's Avatar
    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.

  7. #7
    SitePoint Member vilStewart's Avatar
    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!
    stewart::howe
    Web Developer & Programmer
    CelerMedia.Com | iAmStewart.com | CelerLabs.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •