Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing double-click bug with checkbox + label combination

Note this issue may not apply to the general public, as it does not occur unless you're a fast clicker. (150-200ms/click) The reason I'm posting this issue is because my application has a form with 20+ checkboxes next to each other, and after extensive research I've found no related questions on this matter.

Here's a simplified scenario - 4 checkboxes and 4 labels, one for each checkbox id:

[CB1] Label 1
[CB2] Label 2
[CB3] Label 3
[CB4] Label 4

Assume in each case all CBs are unchecked.

Expected Behavior:

  1. I click on the 4 CBs in rapid succession, they will all become checked. (true)
  2. I click on the 4 Labels in rapid succession, and the corresponding CBs become checked. (only true for Chrome, but still not optimal)

Actual Behavior for case 2 on Win 7 (clicking on labels, because as you know, labels are big and style-able, and the checkboxes are tiny and OS-dependent):

  1. (In Firefox 19) CB2 and CB4 are left unchecked, and while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  2. (In Chrome 26) All CBs get correctly checked, but while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  3. (In IE 10) CB2 and CB4 are left unchecked, but no false highlighting.

The erroneous behavior could be justified if the clicks are on the same element. In our case those are clearly unique checkboxes with different IDs and Names. So the results are wildly unexpected.

So my question is:

Is there a way to disable firing the double click event when I rapidly click on the different checkboxes, but yet still check them with fast single clicks?

The closest I've come to is the following script, which interestingly made Firefox behave like Chrome, and Chrome behave like Firefox:

jQuery(document).on('dblclick', 'input:checkbox+label', function(event){
    console.log('ugly hack fired');
    $(this).click();
    event.preventDefault();
})
like image 918
Scott Yang Avatar asked Apr 06 '13 10:04

Scott Yang


3 Answers

Finally got one very ugly hack that worked for all the browsers, hopefully this will help anyone else who comes across the problem:

Disable selection with css because doing it in JS is simply too inefficient:

.form_class input[type=checkbox] + label{
    -webkit-user-select:none;
    -khtml-user-select:none;
    -moz-user-select:none;
    -o-user-select:none;
    user-select:none;
}

Prevent ALL clicking in JS, and manually do what clicking should do:

jQuery(document).on('click', '.form_class input:checkbox+label', function(event){
    // Assuming label comes after checkbox
    $(this).prev('input').prop("checked", function(i, val){
        return !val;
    });
    event.preventDefault();
})
like image 157
Scott Yang Avatar answered Nov 15 '22 16:11

Scott Yang


This would do it-

$("input[type='checkbox']").dblclick(function (event)
{
    event.preventDefault();
});
like image 41
Kevin Lynch Avatar answered Nov 15 '22 16:11

Kevin Lynch


Try this:

$(document).on('dblclick', 'input:checkbox, label', function (event) {
    event.preventDefault();
    // Your code goes here   
})

OR

$("input:checkbox, label").dblclick(function (event) {
    event.preventDefault();
    // Your code goes here
});

OR

$('input:checkbox').add('label').dblclick(function (event) {
    event.preventDefault();
    // Your code goes here
});
like image 39
palaѕн Avatar answered Nov 15 '22 16:11

palaѕн