In fact, there is no indeterminate
property that makes checkbox go into undetermined state, this is because it is not a state . indeterminate
is merely visual.
The possible states of a checkbox are just true or false or off / em>, 1 or 0, or any equivalent variation). The indeterminate
field is just a tool for web developer to indicate to the user that the data that the application received is insufficient to gauge the real state of the field.
Example : Data from a product publication in some marketplace . There is a checkbox on the screen that defines whether or not the product is published in the external tool. The actual state of the field is defined through a marketplace API call, but the call fails for any reason. The application will be unable to define whether or not the product is published and any state that is placed in the checkbox could confuse the user, pseudo-state indeterminate
.
The only way to define a checkbox field 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">
According to the WHATWG specification, the pseudo-state indeterminate
is just visual and does not overwrite the current state of the field.
The control is never true tri-state control, even if the indeterminate element 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 above example the field is not selected, but if you add the checked property, 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 defined by the checked property, regardless of 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's requirement is not to submit undetermined fields to avoid any type of data conflict, the validation logic must occur exclusively on the client side with JavaScript. The server will not know if the field was submitted as undetermined or not. One way to assist the user is to apply a style with CSS in undefined fields using :indeterminate
:
const checkbox = document.querySelector('input[type=checkbox]')
checkbox.indeterminate = true;
:indeterminate + label {
color: purple;
}
<input type="checkbox">
<label>Publicado?</label>