"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

Struggling to showcase JSON information, encountering a JSON Parsing issue

I'm currently facing an issue with displaying JSON data on my screen, specifically the integer 4. Unfortunately, I keep encountering a JSON Parse Error whenever I try to retrieve and show the data. Even though the data appears to be successfully pos ...

"Unlock the Power of Reactjs Material: Simplifying the Process of Passing Props to Reactjs Material Dialog

Is there a way to pass props to a material dialog in ReactJS? When I try < FormDialog value={this.prop.value} />, I get an error saying Type '{ value: any; }' is not assignable to type 'IntrinsicAttributes'. How can I specify the ...

Is there a way to display an ad every third time a screen is entered in React Native?

I have a screen with recipe details and I'm looking to incorporate interstitial ads that appear every third time a user visits this page. The issue I'm facing is that the ad shows up again if the user exits and returns, which I'd like to avo ...

Error: The object type for web3.eth.Contract is invalid and cannot be used as

Can someone help me troubleshoot these web3.js errors I'm encountering? See the snapshot of the error message here const web3Token = new web3.eth.Contract(ERC20Token.abi); ...

Having issues displaying the & symbol in HTML from backend data

I recently worked on a project using express handlebars, where I was fetching data from the YouTube API. However, the titles of the data contained special characters such as '# (the ' symbol) and & (the & symbol). When attempting to render the ...

What are the reasons behind the lack of smooth functionality in the Bootstrap 4 slider?

My customized bootstrap4 slider is functional, but lacks smoothness when clicking on the "next" and "prev" buttons. The slider transitions suddenly instead of smoothly. Any suggestions on how to fix this? Here is the code for the slider: $('.carous ...

Tips for refreshing the direction route in google-maps-react

I have an array of coordinates, and when I add a new coordinate to the array, I want the route direction on the map to update accordingly. This is the code snippet from my googlemap.js file: /* global google */ import React, { Component } from "react ...

Is the component experiencing issues with its style functionality?

I am attempting to add CSS styles to app.component.ts using the :host property, but I am not seeing the desired results. The CSS is being applied, but not correctly. .ts export class AppComponent { title = "Default Title" data = 'This is defaul ...

Iconic Material Design from Material-UI

Adding an icon button in the form of a star and wanting to fill it with a solid color on click. import StarIcon from '@material-ui/icons/StarBorder' ... <IconButton><StarIcon className={classes.favoricon}/></IconButton> I sear ...

Empower the parent container to wrap around its child elements using Flexbox

I am trying to make the flexbox parent item adjust its width based on the children, but currently it is stretching out to full width and leaving empty space on the right side. Here is the CSS code I have tried: .flexParent { display:flex; background:# ...

Instant Access to a Precise Slide

Using a slider named mySlider or SL_Slider, which is powered by the MooTools library. When on the page with the slider, there is a simple script for the href to call the appropriate slide: <a href="javascript:mySlider.numPress(3);">link</a> ...

Using HTML <style> in a bash script is a great way to enhance

I am attempting to eliminate the bullet points from HTML code generated by a bash script. I am currently struggling to apply the style across the entire program. Given my limited knowledge of html, I suspect that I may be misinterpreting or incorrectly p ...

Problem of special characters not displaying properly in jQuery HTML code

I have encountered an issue with displaying special characters correctly. Despite my efforts, the characters are not showing up as intended. Here is how I defined it: var grade_value = 'Wešto izšamešđu'; Following that, I execute this code ...

Developing a GraphQL search feature in a React Native application

I am struggling to create a search feature that can search by name or category. I require assistance with the backend part, specifically configuring the resolver and index, as I am unsure if my current setup is correct. Resolver.js //search for single use ...

Trouble with Material-UI Textfield Hinttext Functionality

When designing a textfield according to job requirements, I encountered an issue. After assigning a background color to the TextField, the hintText disappeared. To resolve this, I had to manually set the z-index of the label. Now, the hintText is visible, ...

What is causing additional render cycles in this component?

Can anyone help me figure out why I'm experiencing extra re-renders within this component? The issue seems to be occurring internally in the component itself, not from a parent component. Whenever the state changes, it logs "Inside preview container" ...

When you set the href attribute, you are essentially changing the destination of the link

UPDATE: I'm not referring to the status bar; instead, I'm talking about the clickable text that leads to a link. /UPDATE I've gone through a few similar posts but couldn't find a solution. I have a link that needs to be displayed. www ...

Converting TypeScript to JavaScript: A Step-by-Step Guide

I have this code written in Typescript and I need to convert it to JavaScript const Home = (props) => { return ( <div> {props.name ? 'Hi ' + props.name : 'You are not logged in'} </div> ); }; How can I re ...

Prevent page refresh when submitting a form using PureCSS while still keeping form validation intact

I'm currently implementing PureCSS forms into my web application and facing a challenge with maintaining the stylish form validation while preventing the page from reloading. I discovered that I can prevent the page reload by using onsubmit="return f ...

Learn how to efficiently redirect users without losing any valuable data after they sign up using localStorage

I am currently facing an issue with my sign up form. Whenever a user creates an account, I use localStorage to save the form values. However, if the user is redirected to another page after hitting the submit button, only the last user's data is saved ...