"The functionality of the personalized checkbox is not working as expected

Having an issue with my custom checkbox where only the first one gets selected when I select the bottom checkbox. I suspect it might be a styling problem, but I'm unsure. I've recreated the issue in a sandbox for you to check out: https://codesandbox.io/s/adoring-pine-hqxwt

<label htmlFor="checkbox">
    <div className="checkbox-container">
      <input
        className="checkbox-hidden"
        id="checkbox"
        type="checkbox"
        checked={checked}
        onChange={onchange}
      />
      <div className="checkbox-styled">
        <svg
          className="checkbox-icon"
          style={{ visibility: checked ? "visible" : "hidden" }}
          viewBox="0 0 24 24"
        >
          <polyline points="20 6 9 17 4 12" />
        </svg>
      </div>
    </div>
    <span>{label}</span>
  </label>


.checkbox-icon {
  fill: none;
  stroke: #177ff0;
  stroke-width: 2px;
}

.checkbox-container {
  display: inline-block;
  vertical-align: middle;
}
.checkbox-hidden {
  border: 0;
  clip: rect(0 0 0 0);
  // clippath: inset(50%);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

.checkbox-styled {
  display: inline-block;
  width: 16px;
  height: 16px;
  background: salmon;
  border-radius: 3px;
  transition: all 150ms;
  border-radius: 1px;
  border: 1px solid #dbeaf4;
  background-color: transparent;
  :hover {
    border-color: #4da1ff;
  }
}

Answer №1

All checkboxes should have unique values for their id and htmlFor. Each pair of id and htmlFor must be distinct:

const Checkbox = ({ checked, onchange, label, id }) => (
  <label htmlFor={id}>
    <div className="checkbox-container">
      <input
        className="checkbox-hidden"
        id={id}
        type="checkbox"
        checked={checked}
        onChange={onchange}
      />
      <div className="checkbox-styled">
        <svg
          className="checkbox-icon"
          style={{ visibility: checked ? "visible" : "hidden" }}
          viewBox="0 0 24 24"
        >
          <polyline points="20 6 9 17 4 12" />
        </svg>
      </div>
    </div>
    <span>{label}</span>
  </label>
);

Example of how to use it:

<Checkbox
  id={`checkbox${idx}`}
  checked={idx === this.state.isChecked}
  onchange={() => this.toggleCheckbox(idx)}
  label={item}
/>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I need to mass upload a collection of resumes stored in a zip file, then extract and display the content of each resume using a combination of HTML

I recently used a service to extract and retrieve the contents of a zip file. I am trying to read the content of the files and integrate them into the scope of my Angular project. Any suggestions would be greatly appreciated. Below is an outline of my func ...

Secondary menu supersedes primary dropdown menu

Having trouble getting a vertical dropdown menu to work properly, with the submenus overriding the main menu. I've searched everywhere for a solution but can't seem to find one. Anyone out there who could help me figure this out? Here's the ...

Fade-In and Fade-Out CSS Effect with Background Images Displaying a Blank Last Image

I've been experimenting with applying a CSS-only background image transition to a div, but I encountered an issue where after cycling through three specified images, the background reverts back to the original black color. Even stranger, when I adjust ...

Trigger animation once you've scrolled past a designated point in the document

I created a count-up counter animation using JavaScript, but the issue is that the counter starts animating as soon as I refresh the page regardless of where I am on the page or if the counter is even visible. I would like the counter to only start workin ...

What is the best way to navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

Utilizing the Vuex/Redux store pattern to efficiently share a centralized source of data between parent and child components, allowing for customizable variations of the data as

Understanding the advantages of utilizing a store pattern and establishing a single source of truth for data shared across components in an application is essential. Making API calls in a store action that can be called by components, rather than making se ...

Why is a question mark added to the URL when the login button is clicked

I've encountered an issue where clicking this code for the first time redirects me to localhost:8080//? instead of localhost:8080//admin.html. Any idea why this is happening? $("#admin-login").click(function(){ var data = {"username": $("#admin ...

Using a varnish proxy to transmit server-sent events

My website is currently operating behind a Varnish proxy, but I am experiencing issues with server-sent events. The connections remain open indefinitely without receiving any content, and Varnish waits for the stream to end before forwarding it to the brow ...

Storing the background color in a JavaScript variable

I've been experimenting with creating a fade in and out effect for a background image on a website. I've also been attempting to capture the background color of a div and store it in a variable. Here's what I have tried: elem = document.ge ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

Toggle the hamburger menu using JavaScript

How can I close my hamburger menu when clicking a link for one page navigation? The menu is functioning properly, but I need a way to close it. Unfortunately, I have limited knowledge of JS. I only have the HTML and CSS for this: HTML in index.html file ...

Using knockoutjs to call a component on the home page

I am currently facing some challenges with knockoutjs components, as I am following the guidance provided in the official knockout component documentation. Could someone please advise me on how to correctly invoke my widget component on the main page? It ...

Encountered an error during the production build of NEXTJS, where it panicked due to the global thread pool not being initialized

While hosting my app on Heroku and running git push heroku main, I encountered the following error: panicked at 'the global thread pool has not been initialized.: threadpool builderror { kind: ioerror(error { kind: unsupported, message: "operatio ...

Best practices for implementing useDispatch and bindActionCreators in your project?

I have a question about the placement of certain methods. When using Redux store in multiple components, I find myself repeating the same import process: import { useSelector, useDispatch } from "react-redux"; import { bindActionCreators } from & ...

Error appears when MUI Component is added to Component Library and states "Hooks can only be called within the body of a functional component"

I'm facing an issue with my component library that exports various components // Test.tsx import React from 'react'; const Test = () => <p>Test</p>; export default Test; and // TestB.tsx import React from 'react'; i ...

Utilize Ajax, jQuery, and HTML select elements to showcase customized information

Hey everyone, I could really use some assistance with this. What I'm trying to accomplish is selecting data from a database using the onchange() function in Ajax and then returning the result to the client. I have successfully used Ajax to send inform ...

What is the most effective way to loop and render elements within JSX?

Trying to achieve this functionality: import React from 'react'; export default class HelloWorld extends React.Component { public render(): JSX.Element { let elements = {"0": "aaaaa"}; return ( ...

Problem with CSS dotted borders appearing as dashed in Chrome when applied to adjacent columns in a table

After testing the code on both Chrome and Firefox browsers, I noticed that they display the border differently. In Chrome, there is a dash in between adjacent cells while in Firefox, there are no dashes rendering. Can anyone suggest a solution to fix thi ...

Issue encountered while transferring JSON array data into the Material UI React table

import React, {Component} from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Table from '@material-ui/core/Table'; import TableBody from '@material-ui/core/TableBody'; import TableCell from & ...

Upcoming construction: Issue encountered - The Babel loader in Next.js is unable to process .mjs or .cjs configuration files

Within my package.json file, I have set "type": "module" and "next": "^12.2.5". In my tsconfig.json: { "compilerOptions": { "target": "ES2022", "module": "esnext ...