Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mouse position when focus/blur events are fired?

I'm using jQuery to capture an event:

$('input').focus(function(e){

    console.log( e.pageX, e.pageY );

});

This doesn't seem to work... any ideas on alternative ways to getting the mouse position?


Help would be great.

like image 454
RadiantHex Avatar asked Nov 02 '11 17:11

RadiantHex


People also ask

Which event is fired when an element loses its focus?

The focusout event fires when an element has lost focus, after the blur event. The two events differ in that focusout bubbles, while blur does not. The opposite of focusout is the focusin event, which fires when the element has received focus.

What is the difference between focus and blur events?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

Which event is fired when an element loses its focus in HTML 5.0 document?

The onblur event occurs when an object loses focus.

Which event is raised if the user leaves focus from a text box in the page?

Events focus/blur The focus event is called on focusing, and blur – when the element loses the focus. Let's use them for validation of an input field.


1 Answers

You can only get mouse coordinates using mouse events. If you want to capture the position of the mouse, you can use a global mousemove event listener, and store the coordinates in a set of variables, which can later be accessed by the focus function. Example:

var pageX, pageY; //Declare these globally
$(window).mousemove(function(e){
    pagex = e.pageX;
    pageY = e.pageY;
});

$('input').focus(function(){
    console.log(pageX, pageY); // These variables have been defined by the global
                               //  mousemove event
});
like image 154
Rob W Avatar answered Sep 20 '22 15:09

Rob W