Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding JavaScript originalEvent [duplicate]

Tags:

Can someone help me understand the use of originalEvent in JavaScript? I really can't find a good source of documentation about it.

Google results led me to discussion sites that were too complicated to understand for a newbie.

I recently had a question in SO, and a guy answered it by adding this line of code

$("#url").bind('paste', function(e) {     var val = e.originalEvent.clipboardData.getData('text/plain');  .... 

to my existing code, which worked btw.

I would greatly appreciate if someone can help me understand the use of it.

like image 767
bobbyjones Avatar asked Oct 27 '13 12:10

bobbyjones


2 Answers

You are using a JavaScript library called jQuery, which is where the $() function comes from. jQuery wraps several parts of JavaScript to make it easier to use. One of those parts is event handling. In your example, because you're using jQuery to bind to the paste event, the object passed to your callback (e) is a jQuery event object, not a built-in JavaScript event object. The jQuery event object exposes the originalEvent property to give you access to the underlying built-in event object.

In your example you need to get hold of the clipboard data, which isn't available through the jQuery event object so you need to access the original event object to get at it.

like image 132
joews Avatar answered Sep 24 '22 16:09

joews


Well...originalEvent is not directly from javascript as per my knowledge. it is the one that is triggered by browser. Jquery wraps up some more properties and the original event from browser is wrapped in originalEvent. Here is what i found from jquery site.

"It's also important to note that the event object contains a property called originalEvent, which is the event object that the browser itself created. jQuery wraps this native event object with some useful methods and properties, but in some instances, you'll need to access the original event via event.originalEvent for instance. This is especially useful for touch events on mobile devices and tablets."

like image 23
thecodejack Avatar answered Sep 21 '22 16:09

thecodejack