How to determine the focus event on jQuery if the focus is fired on click event or tabstop? I have this focus event that if the focus is fired by a tabstop I will execute something and if it is a click I will not execute it.
Here's a pseudo code
$('a').focus(function(){
if(ThisIsTabStop()) {
IWillExecuteThis();
}
});
Triggering a click synthetically does not cause focus to switch to the element, however the click triggered by hardware does.
To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked. Here is the HTML for the examples in this article.
The focus event fires when an element has received focus. The event does not bubble, but the related focusin event that follows does bubble. The opposite of focus is the blur event, which fires when the element has lost focus. The focus event is not cancelable.
The focusin event fires when an element has received focus, after the focus event. The two events differ in that focusin bubbles, while focus does not. The opposite of focusin is the focusout event, which fires when the element has lost focus. The focusin event is not cancelable.
If an element is clicked, the mousedown
event fires before focus. You just need to set a data attribute, and check it in the focus event.
Try this demo: http://jsfiddle.net/KcGcF/1/
$('a').mousedown(function(e){
var $this = $(this);
// Only set the data attribute if it's not already focused, as the
// focus event wouldn't fire afterwards, leaving the flag set.
if(!$this.is(':focus')){
$this.data('mdown',true);
}
});
$('a').focus(function(e){
var $this = $(this);
var mdown = $this.data('mdown');
// Remove the flag so we don't have it set next time if the user
// uses the tab key to come back.
$this.removeData('mdown');
// Check if the mousedown event fired before the focus
if(mdown){
// Click
} else {
// Tab
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With