Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with browser differences with indeterminate checkbox

Tags:

Primer: An HTML checkbox can be set as indeterminate, which displays it as neither checked nor unchecked. Even in this indeterminate state, there is still an underlying boolean checked state.

Mac OS X checkboxes in various states


When an indeterminate checkbox is clicked, it loses its indeterminate state. Depending on the browser (Firefox), it can additionally toggle the checked property.

This jsfiddle illustrates the situation. In Firefox, clicking either of the checkboxes once causes them to toggle their initial underlying checked state. In IE, the checked property is left alone for the first click.

I would like all browsers to behave the same, even if this means additional javascript. Unfortunately, the indeterminate property is set to false before the onclick handler (or onchange and jquery change) is called, so I can't detect whether it's called for a click on an indeterminate checkbox or not.

The mouseup and keyup (for spacebar toggle) events show the prior indeterminate state, but I'd rather not be that specific: it seems fragile.

I could maintain a separate property on the checkbox (data-indeterminate or similar), but I wanted to know if there's a simple solution I'm missing, and/or if other people are having similar issues.

like image 202
aaaidan Avatar asked Apr 11 '12 02:04

aaaidan


People also ask

What happens when you click an indeterminate checkbox?

A checkbox usually has two states: checked and unchecked. But indeterminate checkboxes are in a third state: neither checked nor unchecked. The “checkedness” is not determined.

How do I set an indeterminate checkbox?

A checkbox cannot be set to indeterminate state by an HTML attribute - it must be set by a JavaScript. This state can be used to force the user to check or uncheck the checkbox.

What are the three states of checkbox?

A tri-state checkbox can be checked, not checked, or partially checked. The condition of being partially checked is based on the selection of child elements. If all child elements are selected, the parent checkbox is checked. If some child elements are selected, the parent checkbox is partially checked.

Which of these elements are targeted by the indeterminate selector in css3?

Elements targeted by this selector are: <input type="checkbox"> elements whose indeterminate property is set to true by JavaScript. <input type="radio"> elements, when all radio buttons with the same name value in the form are unchecked.


1 Answers

If you would like to have an inderterminate checkbox which becomes checked on all browsers on click (or at least on IE, Chrome and FF5+), you need to initialise the checked attribute correctly, as shown here http://jsfiddle.net/K6nrT/6/. I have written the following functions to help you:

/// Gives a checkbox the inderminate state and the right /// checked state so that it becomes checked on click /// on click on IE, Chrome and Firefox 5+ function makeIndeterminate(checkbox) {     checkbox.checked = getCheckedStateForIndeterminate();     checkbox.indeterminate = true; } 

and the interesting function which relies on feature detection:

/// Determine the checked state to give to a checkbox /// with indeterminate state, so that it becomes checked /// on click on IE, Chrome and Firefox 5+ function getCheckedStateForIndeterminate() {     // Create a unchecked checkbox with indeterminate state     var test = document.createElement("input");     test.type = "checkbox";     test.checked = false;     test.indeterminate = true;      // Try to click the checkbox     var body = document.body;     body.appendChild(test); // Required to work on FF     test.click();     body.removeChild(test); // Required to work on FF      // Check if the checkbox is now checked and cache the result     if (test.checked)     {         getCheckedStateForIndeterminate = function () { return false; };         return false;     }     else     {         getCheckedStateForIndeterminate = function () { return true; };         return true;     } } 

No image tricks, no jQuery, no extra attributes and no event handling involved. This relies only on simple JavaScript initialisation (note that the "indeterminate" attribute cannot be set in the HTML markup, so JavaScript initialisation would have been required anyway).

like image 174
Gyum Fox Avatar answered Oct 16 '22 06:10

Gyum Fox