Results 1 to 5 of 5

Thread: calling an onclick as a function

  1. #1
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Question calling an onclick as a function

    Ok, so I have a situation where I have applied an array of "onclick" functions to different elements.

    I need to call their "onclick" functions as a function rather than an onclick. Strangely enough, this works in IE using the following -

    document.getElementById(something).click();

    Unfortunately, it won't work in firefox... Anyone have any insight into this?
    I contemplated gathering the .onclick, and .splitting the function and recreating it, but I was curious if there is a better solution.


    Any help appreciated.

    - Ben
    Last edited by Falkon303; 10-30-2009 at 05:24 PM.
    document.write is document.wrong

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Do you want this for an if statement, like this:
    Code:
    if(blah.click){
    
    }
    Or.. what?

    The beset way to do it is just this:
    Code:
    document.getElementById(something).onclick = function();
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I may be missing something, but I think Nile is missing the point here.

    It really depends upon how the onclick event was assigned to the element in the first place. If it was hard coded, or if it was assigned sort of like so:

    Code:
    el.onclick = whatever
    el being a reference to the element, whatever being a reference to the function or the function itself. If it was either of those two (really three if you want to get technical about it) ways, you may:

    Code:
    el.onclick();
    However, if the function assigned originally uses the 'this' keyword to refer to the element, then you must do something like so:

    Code:
    el.onclick.apply(el);
    If the event was attached or added (addEventListener or attachEvent), another apprach will probably need to be taken. Similarly, if the event was added via some script library (like mootools, jQuery, Prototype, etc.), that library may or may not have its own way of doing what you want in this regard.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Falkon303 (10-31-2009)

  5. #4
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    I may be missing something, but I think Nile is missing the point here.

    It really depends upon how the onclick event was assigned to the element in the first place. If it was hard coded, or if it was assigned sort of like so:

    Code:
    el.onclick = whatever
    el being a reference to the element, whatever being a reference to the function or the function itself. If it was either of those two (really three if you want to get technical about it) ways, you may:

    Code:
    el.onclick();
    However, if the function assigned originally uses the 'this' keyword to refer to the element, then you must do something like so:

    Code:
    el.onclick.apply(el);
    If the event was attached or added (addEventListener or attachEvent), another apprach will probably need to be taken. Similarly, if the event was added via some script library (like mootools, jQuery, Prototype, etc.), that library may or may not have its own way of doing what you want in this regard.
    After scouring the web for a lil bit, I discovered this same thing.

    For some reason, IE allowed the .click() as a method to call the click function, but the correct method (across all browsers) is ".onclick()".

    Gratzi for this reply

    - Ben
    document.write is document.wrong

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Falkon303 View Post
    For some reason, IE allowed the .click()
    That's a good characterization of it. In IE (perhaps one or more others) the el.click() is more of a shortcut. It's like telling the browser to click on the element. In all others, and in IE, using the el.onclick() simply tells the browser to run the function associated with the element's onclick attribute (if any). There are differences. The main one being like - if you have an ordinary anchor link with no onclick attribute - the el.click() method will fire the link. The el.onclick() will not.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •