How do you capture the location of a cursor via an event listener?
In other words, if my user clicks a button, is it possible to find the location of the cursor at that time? If so, how?
UPDATE I am not sure if I was clear enough. I don't want the mouse location. What I meant to say is that if a cursor is inside an input field on a form, I want to locate the cursor and find out where the FOCUS is.
Of course, when you click on a button, the focus is gone from anywhere else it was. But I seem to understand you want to know where the focus was just before.
You could so something like:
$('input, textarea, select').on('focus', function() {
console.log($(this).attr('id') + ' just got focus!!');
window.last_focus = $(this).
});
$('button').on('click', function() {
console.log('Last focus was on ' + window.last_focus.attr('id'));
});
This is just a very naive implementation. I am also very curious why you would need this?
There is also a focusout
event which is triggered when something loses the focus, which might be more appropriate.