Youstay Igo Youstay Igo - 4 months ago 8
HTML Question

How to check or uncheck checkboxes by clicking on text instead of checkbox itself?

I have seen it on several webpages (cannot recall where exactly atm) where I am able to check or uncheck checkboxes by clicking on the text in front of the checkbox. I know how to do it in JavaScript (create a span with onclick()) , but I want to know if there is any way I can do it without JavaScript.

Answer

You can achieve this functionality using label class as given below.

<input type="checkbox" id="option">

<label for="option">Select this option</label>

Comments