Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

jQuery event object

Suggested Videos
Part 28 - Difference between each and map in jquery
Part 29 - jQuery change event
Part 30 - jQuery mouse events

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

convert javascript event to jquery 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.

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

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

<body style="font-family:Arial">
    <input id="btn" type="button" value="Click me" /><br /><br />
    <div id="divResult"></div>

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 = " + +
                        "<br/>Target Tag Name = " +;


jQuery tutorial for beginners

No comments:

Post a Comment

It would be great if you can help share these free resources