Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery firing click event without a click

Well, that's kind of what happens.

$('.js-custom-dropdown').find('.custom-dropdown-unfolded').toggle();
$('.custom-dropdown-btn, .custom-dropdown-btn-unfolded').keydown(function(event){
    if (event.keyCode === 13) {
        openDropdown($(this));
    }
}).click( function(){
    openDropdown($(this));
});

function openDropdown (element){
    element.parents('.js-custom-dropdown').find('.custom-dropdown-unfolded').toggle();
    console.log($(this))
}

When I click the dropdown button, openDropdown function is executed once, but when I Tab my way to the button and press enter, the function is called twice. Guess that has something to do with chaining, but I admit that I'm new to this and don't fully understand jQuery design patterns. I could of cource call the function twice in keydown handler and that would solve the problem, but.. you know :)

Could you please explain what's wrong with the code and what causes such behavior?

like image 578
Anton Matyulkov Avatar asked Mar 27 '13 09:03

Anton Matyulkov


People also ask

How to trigger click event without clicking in JavaScript?

If you want native JS to trigger click event without clicking then use the element id and click () method of JavaScript. Create one button with “trigger” id and assign msg () function to the onClick attribute. If the button clicks then msg () function triggers a popup alert.

What is click handler in jQuery?

.click( handler )Returns: jQuery. Description: Bind an event handler to the "click". JavaScript event, or trigger that event on an element. version added: 1.0.click( handler ) handler. Type: Function( Event eventObject ) A function to execute each time the event is triggered.

What is a click event?

Definition and Usage The click event occurs when an element is clicked. The click () method triggers the click event, or attaches a function to run when a click event occurs.

What is the use of click () method in JavaScript?

The click() method triggers the click event, or attaches a function to run when a click event occurs.


2 Answers

This is down to html 5 user interactions specifications. If you take a look at the HTML spec for elements with a tabIndex you'll find they mention that for any element with a tab index the enter key causes a click event.

I suspect that is what's causing this behaviour. You could try it on an older (non-html 5 compliant) browser to test whether this is the case.

Edit (as requested by poster): The answer given by @Terry provides a way to remedy the issue you're having. Using jquery's "preventDefault" on the event will stop it being hit twice when you hit enter.

like image 79
Timbo Avatar answered Oct 07 '22 13:10

Timbo


You can trigger the click event on keydown using: $(this).trigger('click');, and the preventing the default action upon keydown:

$('.custom-dropdown-btn, .custom-dropdown-btn-unfolded').keydown(function (e) {
    if (e.keyCode === 13) {
        $(this).trigger('click');
        e.preventDefault();
    }
}).click(function () {
    openDropdown($(this));
});

Here's a proof-of-concept fiddle: http://jsfiddle.net/yTuR3/

like image 44
Terry Avatar answered Oct 07 '22 12:10

Terry