PDA

View Full Version : calling an onclick as a function



Falkon303
10-30-2009, 05:06 PM
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

Nile
10-30-2009, 05:36 PM
Do you want this for an if statement, like this:


if(blah.click){

}
Or.. what?

The beset way to do it is just this:


document.getElementById(something).onclick = function();

jscheuer1
10-31-2009, 05:24 AM
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:


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:


el.onclick();

However, if the function assigned originally uses the 'this' keyword to refer to the element, then you must do something like so:


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.

Falkon303
10-31-2009, 08:40 AM
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:


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:


el.onclick();

However, if the function assigned originally uses the 'this' keyword to refer to the element, then you must do something like so:


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

jscheuer1
10-31-2009, 01:31 PM
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.