Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: event is not defined in Firefox

I am using this function:

$("#tooltip").animate({ left: event.clientX, top: event.clientY });

Its working on Chrome and IE but in Firefox I get this error:

ReferenceError: event is not defined $("#tooltip").animate({ left:
event.clientX, top: event.clientY });

How can i solve this?

like image 520
mothana Avatar asked May 29 '26 10:05

mothana


2 Answers

You appear to be trying to use the odd global event object that is a legacy of the 4.0-browser-era Microsoft event model. Don't. Use the standard event object instead. It will be the first argument to your event handler function.

jQuery('selector').on('click', function (evt) {
    jQuery("#tooltip").animate({ left: evt.clientX, top: evt.clientY });
});
like image 51
Quentin Avatar answered May 31 '26 23:05

Quentin


Mothana, Quentin is right,almost. I work with d3.js (jQuery like library) and the old story browser thing is that events that are not declared are not acceptable. So you have 3 weays to solve this:

1. Don't use events - not our case i think.

2. Use the standard event object of your function - Example in Quentin's answer.

3. Make your javascript library object do the work for you. Example in d3.js:
I had this not working code:

    tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px"))

and i used the d3.js object named "d3" to make the selection of the event for me (working code):

tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px")

So I suppose in jQuery you must use the name of your javascript library object, something like jQuery.someSelection().

like image 43
Igorovsky Avatar answered May 31 '26 22:05

Igorovsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!