In fact, there is no property indeterminate
that makes the checkbox enter the undetermined state, that because it’s not a state. The indeterminate
is merely visual.
The possible states of a checkbox are only true or false (on or off, 1 or 0, or any equivalent variation). The field indeterminate
is just a tool for the web developer indicate to the user that the data the application received are insufficient to measure the actual state of the field.
Example: data from a product publication on any Marketplace. There’s on screen one checkbox which defines whether the product is published or not in the external tool. The actual state of the field is defined by calling the Marketplace, but the call fails, for whatever reason. A application will be unable to define whether the product is, or not, published and any state that is placed on checkbox could confuse the user, so the pseudo-state indeterminate
.
The only way to define a field checkbox as undetermined is through Javascript, with elemento.indeterminate = true
.
const checkbox = document.querySelector('input[type=checkbox]')
checkbox.indeterminate = true;
console.log(checkbox.checked);
<input type="checkbox">
As per the specification WHATWG, the pseudo-state indeterminate
is just visual and does not overwrite the current state of the field.
The control is Never a true tri-state control, Even if the element’s indeterminate IDL attribute is set to true. The indeterminate IDL attribute only gives the Appearance of a third state.
That’s why states are just true or false. Note that in the example above it is given that the field is not selected, but if adding the property checked, the return changes, regardless of whether it is undetermined or not.
const checkbox = document.querySelector('input[type=checkbox]')
checkbox.indeterminate = true;
console.log(checkbox.checked);
<input type="checkbox" checked>
The form being submitted, the value sent by the browser will be as defined by the property checked, whether the field is undetermined or not. In the first example the browser would submit the field as false while in the second as true.
If the application is required not to submit indeterminate fields to avoid any kind of conflict between the data, the validation logic should occur exclusively on the client side, with Javascript. The server will have no way of knowing whether the field was submitted as undetermined or not. One way to help the user is by applying a CSS style to the undetermined fields using :indeterminate
:
const checkbox = document.querySelector('input[type=checkbox]')
checkbox.indeterminate = true;
:indeterminate + label {
color: purple;
}
<input type="checkbox">
<label>Publicado?</label>
Now it’s clearer how to use this pseudo-state! Thank you
– hugocsl