Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially select checkbox with javascript

Is there a way using Javascript and HTML(5) to make a checkbox be partially selected, like the checkboxes in a program installation menu when you select only some of the sub options?

like image 301
AsherMaximum Avatar asked Jun 07 '11 17:06

AsherMaximum


People also ask

How do I set an indeterminate checkbox?

Checkboxes actually has three states: true, false and indeterminate which indicates that a checkbox is neither "on" or "off". 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 happens when you click an indeterminate checkbox?

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. When an indeterminate checkbox is clicked, it loses its indeterminate state.

How do you checkbox is checked or not in JavaScript?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.


1 Answers

HTML5 defines the indeterminate boolean property.

I've tested it only in the latest Safari, Chrome, and Firefox. They all support the indeterminate state.

Example: http://jsfiddle.net/5tpXc/

Edit: Note that this wont work with older browsers and maybe not with current versions of IE. You'd need to rely on hacks as described in other answers here if you want to support all browsers.

like image 108
kassens Avatar answered Oct 09 '22 19:10

kassens