jQuery event object
Edit
  Murat
57

     
0 0

Published on 04-08-2017
Link for all dot net and sql server video tutorial playlistshttps://www.youtube.com/user/kudvenkat/playlists?sort=dd&view=1Link for slides, code samples and text version of the
Link for all dot net and sql server video tutorial playlists
https://www.youtube.com/user/kudvenkat/playlists?sort=dd&view=1

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2015/04/jquery-event-object.html

In this video we will discuss
1. How to retrieve event data using event object
2. How to convert JavaScript event object to jQuery event object

Whenever an event (like click, mouseover, mouseout etc) occurs, the relevant data about that event is placed into the event object. For example, the event object contains event data like, the X and Y coordinates of the mouse pointer when the event occurred, the HTML element that fired the event, which mouse button is clicked etc.

Obtaining the event object is straightforward. The event object is always passed to the event handler method. Let us understand this with an example. When we click the button, we want to capture the following event data
1. Event name
2. Mouse X coordinate when the event occured
3. Mouse Y coordinate when the event occured
4. The control that raised the event
5. The HTML tag name that raised the event

Notice that in the example below, we are passing event object to getEventDetails() method. This object is the raw JavaScript event object. The type property gives us the event name that occured. pageX and pageY properties return the X and Y coordinates of the mouse pointer. Target property returns the HTML element that raised the event. Target, pageX and pageY properties are supported by all modern browsers and Internet Explorer 9 and above. The following code will not work in Internet Explorer 8 and earlier versions. In addition to click event, the following example returns mouseover and mouseout event data.

Replace < with LESSTHAN symbol and > with GREATERTHAN symbol

<html>
<head>
<title></title>
<script src="jquery-1.11.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btn').click(function () {
getEventDetails(event);
}).mouseover(function () {
getEventDetails(event);
}).mouseout(function () {
getEventDetails(event);
});

function getEventDetails(event) {
var eventDetails = "Event = " + event.type +
"<br/> X = " + event.pageX + "<br/>Y = " +
event.pageY + "<br/>Target Type = " + event.target.type +
"<br/>Target Tag Name = " + event.target.tagName;

$('#divResult').html(eventDetails);
}
});
</script>
</head>
<body style="font-family:Arial">
<input id="btn" type="button" value="Click me" /><br /><br />
<div id="divResult"></div>
</body>
</html>

Cross-browser solution : For the above code to work in all browsers including Internet Explorer 8 and earlier versions, modify getEventDetails() function as shown below. Notice that we are converting JavaScript event object to jQuery event object using $.event.fix()

function getEventDetails(event) {
var e = $.event.fix(event);
var eventDetails = "Event = " + e.type +
"<br/> X = " + e.pageX + "<br/>Y = " +
e.pageY + "<br/>Target Type = " + e.target.type +
"<br/>Target Tag Name = " + e.target.tagName;

$('#divResult').html(eventDetails);
}


COMMENTS · 0

Big Grin :D Frown :( Mad :mad: Stick Out Tongue :p Embarrassment :o Confused :confused: Wink ;) Smile :) Roll Eyes (Sarcastic) :rolleyes: Cool :cool: EEK! :eek:
  


jQuery event object Link for all dot net and sql server video tutorial playlistshttps://www.youtube.com/user/kudvenkat/playlists?sort=dd&view=1Link for slides, code samples and text version of the convert javascript event to jquery,convert javascript event to jquery event,convert js event to jquery event,jquery event object example,jquery event object target,jquery mouse x y position,jquery event target tagname,jquery event target type,jquery event target element type,jquery tutorial,jquery tutorial for beginners guest Guest 0 1 core/images/default/default_avatar_thumb.png http://vbtube.com/community 63 20 0 No more comments Youtube-Browser 0
Working...
X