Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery HTML click event firing twice

I have a custom select menu button, and I have bound to the html click event to close it. But the event is firing twice.

http://jsfiddle.net/GnzBj/1/

$(function () {
    $('html').click(function () {
        console.log('html');
    });
});

Any one know why/how to prevent it firing twice?

like image 329
Petah Avatar asked Jan 24 '26 15:01

Petah


1 Answers

The reason why the event is triggered twice is because you have the whole UI inside a label.

<div  ...>
    <label for="xmod-form-51183d51afa3d" ... >
        <select name="theme" id="xmod-form-51183d51afa3d" ...>
            ...
        </select>
        ...
    </label>
</div>

Clicking on the label will also trigger a click event on the form element it relates to.

If you remove the label element, it works as expected: http://jsfiddle.net/GnzBj/5/.

In case you need the label, let it contain as few as possible of the UI, but clicking on it will still trigger two events.

like image 170
Felix Kling Avatar answered Jan 26 '26 03:01

Felix Kling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!