Difference between click and fireEvent

715 views
Skip to first unread message

berlinsurfer

unread,
Aug 29, 2010, 11:02:12 AM8/29/10
to MooTools Users
Hi,
just a short question: is there a difference beween firing the click
event (el.fireEvent('click');) or actually really clicking it ?
Thank you very much in advance,
Jan

Oskar Krawczyk

unread,
Aug 29, 2010, 1:16:41 PM8/29/10
to mootool...@googlegroups.com
Yes, when you fire the click event, no event properties are being passed - unless you specify them manually - so the following would throw an error:

el.addEvent('click', function(e){
e.stop(); // error here
});

berlinsurfer

unread,
Aug 29, 2010, 11:42:40 PM8/29/10
to MooTools Users
Thank you very much Oskar.

> - unless you specify them manually -
How would I possibly do that ? I have seen that a script relies on the
event being passed and it's definately easier to generate an event
than to refactor the class...
I have found http://mootools.net/docs/core/Native/Event but which
parameters do I have to use for a simple click event ?
Cheers,
Jan

Barry van Oudtshoorn

unread,
Aug 30, 2010, 1:34:39 AM8/30/10
to mootool...@googlegroups.com
On 30/08/10 11:42, berlinsurfer wrote:
> How would I possibly do that ? I have seen that a script relies on the
> event being passed and it's definately easier to generate an event
> than to refactor the class...
> I have found http://mootools.net/docs/core/Native/Event but which
> parameters do I have to use for a simple click event ?
> Cheers,
> Jan

The simplest way to do it is to look at the functions that are handling
the click, and create an anonymous object with stubs. That is to say,

blah.fireEvent('click', {
stop: $empty,
preventDefault: $empty,
// etc.
});

--
Barry van Oudtshoorn
www.barryvan.com.au

Not sent from my Apple πPhone.

אריה גלזר

unread,
Aug 30, 2010, 12:57:09 PM8/30/10
to mootool...@googlegroups.com
I'm pretty sure that is why mootools has the Event object:

el.fireEvent('click',new Event);
--
Arieh Glazer
אריה גלזר
052-5348-561
5561

אריה גלזר

unread,
Aug 30, 2010, 1:02:33 PM8/30/10
to mootool...@googlegroups.com
On Mon, Aug 30, 2010 at 7:57 PM, אריה גלזר <arieh....@gmail.com> wrote:
I'm pretty sure that is why mootools has the Event object:

el.fireEvent('click',new Event);


my mistake. I was sure i read somewhere how this can be used but couldn't find the source, and playing with it made me realize Event requires an event as an argument. Any insight from the masters?

Sanford Whiteman

unread,
Aug 31, 2010, 12:23:18 AM8/31/10
to אריה גלזר
> my mistake. I was sure i read somewhere how this can be used but couldn't
> find the source, and playing with it made me realize Event requires an event
> as an argument. Any insight from the masters?

http://jsfiddle.net/gCgXq/2/

Note Event ctor doesn't need a real event in IE.

-- S.

אריה גלזר

unread,
Aug 31, 2010, 1:08:37 AM8/31/10
to mootool...@googlegroups.com
Very Cool. I Think I'll implement this and upload to the Forge (Event.Mock?)

אריה גלזר

unread,
Aug 31, 2010, 2:10:35 AM8/31/10
to mootool...@googlegroups.com
And it's done!

I must say the MDC event spec has a lot of potential. It actually supports bubbling. A shame this cannot be easily implemented on IE, or we could have had a very strong fireEvent implementation...

Rolf -nl

unread,
Sep 1, 2010, 10:04:26 AM9/1/10
to MooTools Users
In what kind of real user case would you need this? When you must act
like you actually clicked a link (or whatever element that has a click
event attached) when you didn't?

On Aug 31, 8:10 am, אריה גלזר <arieh.gla...@gmail.com> wrote:
> And it's done <http://mootools.net/forge/p/event_mock>!

Sanford Whiteman

unread,
Sep 1, 2010, 9:38:58 PM9/1/10
to Rolf -nl
> In what kind of real user case would you need this? When you must
> act like you actually clicked a link (or whatever element that has a
> click event attached) when you didn't?

When you are using a third-party (or inalterable for some other
reason) event handler that expects an Event to be passed and doesn't
create its own mock `e` internally when it's been fake-fired. IOW,
when you want to fire an event handler that depends on an event (even
though the two could/should be separate).

You can fake a duck-typed Event, as Barry suggested, by adding the
minimal props to a simple Object, but this requires inspecting the
handler code in depth and could be really annoying to maintain. Pass
it a real Event and you shouldn't need to worry about faking anything.

-- S.

Reply all
Reply to author
Forward
0 new messages