DEV Community

Berra
Berra

Posted on

I'm clicking on a checkbox label and another checkbox is selected! I have the solution.

You're workin' the checkboxes doing a fancy layout with your label element like this:

<label for="input-id">
<input type="checkbox" id="input-id" />
Enter fullscreen mode Exit fullscreen mode

Now when you're clicking the label the input is selected.

Really nice! And very accessible.

BUT! Remember. If you add more checkboxes. Maybe with complicated names and ids. Make sure you don't have the same id on multiple checkboxes.

In that case the first checkbox with that ID will be selected.

That can be very confusing and lead you to question how React.useState works and other stuff.

Now you know!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more