Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button element not firing click event when clicking on button text and releasing off it (but still inside the button)?

On WebKit browsers (I tested on Chrome and Safari on Mac), button element behaves weird:

Wen in this fiddle http://jsfiddle.net/5ReUn/3/ you do the following:

  1. Press left mouse button while the cursor is over HTML button text
  2. Move the cursor (while pressed) to an area of the button without text
  3. Release the mouse button

Then the click event on the button element is not fired!

HTML is very simple:

<button id="button">Click</button>

And the CSS is not sophisticated at all:

button {
    display: block;
    padding: 20px;
}

JS for click catching:

button = document.getElementById('button');
button.addEventListener('click', function() {
    console.log('click');
});

Many visitors to my site complain that buttons are not clickable. They don't realize they are moving the cursor while clicking.

Has anybody found a workaround for this?

like image 677
Ernests Karlsons Avatar asked Mar 28 '13 10:03

Ernests Karlsons


People also ask

How do I trigger a click event without clicking?

If you want to trigger click event without clicking a button manually, then you should use the click() method in Javascript. Example : click button on page load dynamically. For that we will create a button with an id my-btn . So that, we can select the button using the getElementById() method.

How do you fire a click event on an element?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

Which function is needed to perform a click on button element?

If you want to make a button onclick, you need to add the onclick event attribute to the <button> element.


1 Answers

It turns out to be a bug in WebKit.

An quick non-JavaScript solution is to wrap the text in a SPAN element and make it click-through:

<button>
    <span>Click Me</span>
</button>

Example CSS:

span {
    display: block;
    padding: 20px;
    pointer-events: none;   // < --- Solution!
}

Since the bug appears only in WebKit, browsers that don't support pointer-events can be ignored.

like image 180
Ernests Karlsons Avatar answered Oct 05 '22 11:10

Ernests Karlsons