Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check/uncheck checkbox without using JavaScript/jQuery

Is it possible to toggle the state (check/uncheck) of an input type checkbox without using JavaScript/jQuery i.e. only through HTML and CSS?

Say I want to toggle it on click on a label next to it.

If yes, how can we achieve that?

like image 715
SKSpall Avatar asked May 06 '26 00:05

SKSpall


2 Answers

If you need to check/uncheck the input from a <label>, set an id on the input, and use the label's for attribute to connect them.

<input type="checkbox" id="chk" />

<label for="chk">Click to check/uncheck</label>

Another option is to use the label as a container to the input:

<label><input type="checkbox" /> Click to check/uncheck</label>
like image 158
Ori Drori Avatar answered May 07 '26 12:05

Ori Drori


label and input are meant to work together :)

see : https://www.w3.org/wiki/HTML/Elements/label

The caption can be associated with a specific form control:

  • Using for attribute [Example A]

  • By putting the form control inside the label element itself. [Example B]

<label for="a">toggle state</label><input type="checkbox" id="a"/>
like image 23
G-Cyrillus Avatar answered May 07 '26 14:05

G-Cyrillus