I'm having trouble customizing the appearance of a checkbox. I want it to change color when clicked, but I can't seem to get it to stay in

In order to achieve the desired effect where the white part should appear on the tick, initially the entire thing should be black and then turn white upon clicking. I am currently working on this in Storybook and would appreciate help with positioning. https://i.sstatic.net/NHjPI.png

  <span className={css.cb}>
    <input type="checkbox" value="1" id="checkboxInput" name=""/>
      <label for="checkboxInput"></label>
  </span>

Here is the CSS !

 @import "base.scss";

    .cb {
     display: inline-block;

     position: absolute;
     width: 20px;
     height: 20px;
     left: 10px;
     top: 5px;

     background: #000000;
     border-radius: 5px;
     box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.75);
   }

    .cb label {

    display: inline-block;

    position: absolute;
    width: 12px;
    height: 12px;
    left: 4px;
    top: 10px;

    background: #000000;
    border-radius: 5px;
   }

   .cb input[type=checkbox]:checked + label {
      background: #FFFFFF;
    }

Answer №1

Would you like me to summarize your issue? It seems that you want to alter the class of a span element based on clicking a checkbox. To achieve this, you will require the state of a React class, a method to manage checkbox clicks, and some adjustments to your existing code.

Initiating state

constructor(props){
super(props)
this.state = {
 isChecked = false
}
 this.handleClick = this.handleClick.bind(this)
}

The above snippet could be your class constructor. Below is the proposed method for handling clicks.

handleClick(e){
e.preventDefault()
this.setState({"isChecked":!this.state.isChecked})
}

This method includes the necessary changes for your current code. Here's an example implementation:

<span className={this.state.isChecked?"yourClassForWhiteBackground":"yourClassForBlackBackground"}>
    <input type="checkbox" onClick={this.handleClick} value="1" id="checkboxInput" name="" />
  <label for="checkboxInput"></label>
</span>

I hope this solution aligns with what you were seeking.

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

Ways to determine if the user is either closing the browser or navigating to a different website

I am looking to set up session management in a manner where all sessions are expired or destroyed when the user closes the browser or tab. However, I would like to retain all the sessions if the user is navigating to another website. Is there a way to ac ...

What is the process for updating the h1 header with text entered into an input field?

I'm working on an assignment that requires me to change the h1 heading to reflect whatever is entered into the input field. To accomplish this, I need to create a function using getElementByID. Here's what I've done so far: <!DOCTYPE htm ...

Using Multer to upload an image in a React application

I am currently working on a project with two essential files: the server file and the register file. In the server file, I have included the following code snippet: import multer from 'multer'; const storage = multer.diskStorage({ destinatio ...

Preventing image overlap in Bootstrap when window size is reduced

Here is the code snippet: <div id="members"> <div class="container-fluid"> <div class="row"> <div id="members" class="col-md-12 text-center"> <h3>Text</h3> &l ...

I am seeking assistance in troubleshooting this javascript code. Can someone please help me identify the issue?

function validateCredentials() { var username = document.forms["myForm"]["name"].value; var admin = "admin"; var user = "user"; var master = "master"; if (username == "&qu ...

Trimming the div tag from the bottom of the webpage

Currently utilizing bootstrap 4.0.0-beta.2 and encountering a CSS issue. In the specific layouthttps://i.sstatic.net/7WOGu.png Here is the HTML: <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" re ...

Ways to showcase an icon within a select options tag

Is there a way to show Font Awesome icons in select options tag? I have tried using it outside like this <i class="fas fa-chart-pie"></i> But I'm not sure how to display it within the <option> tags instead of text. <select id="s ...

transfer a product attribute value to a different product attribute within the Magento platform

There is an attribute called Weight : Attribute Code: weight Scope: general Catalog Input Type for Store Owner: Text Field Values Required: yes Apply To: Simple Product, Bundle Product Allow HTML Tags on Frontend: yes Also, there is a General Weight attr ...

SCSS - Seeking a solution to eliminate line number comments from the generated CSS file

In my project on Mac, I incorporated SCSS. The problem at hand is the necessity to eliminate Line Number Comments (/* line Number */) from the output CSS file. Is there a solution available for this issue? ...

Creating a styled slider thumb using Styled Components

Having some trouble styling a slider using styled-components for React. Specifically, I am unsure how to style the thumb of the slider. Here is the CSS code I have: .faderInput::-webkit-slider-thumb { -webkit-appearance: none; width: 15px; heig ...

Having trouble with installing npx -p @storybook/cli sb init for the React Storybook tutorial?

Thank you for taking the time to address my concern. I am currently working through the React Tutorial in Storybook; Setting up our project: Using the command npx create-react-app taskbox Followed by cd taskbox Integrating Storybook: Running npx -p @st ...

Customize the color of arrows within an ng-bootstrap carousel

I am currently working on a carousel using ng-bootstrap, and I need to change the color or image of the arrows from white to black because my background images are white and not visible. The issue is that I am having trouble accessing span, and I am unsure ...

What is the process for obtaining a fresh token from Cognito using the frontend?

Currently, I am working with a react app that utilizes Cognito for user authentication. My main query revolves around making a call to Cognito using the refresh token in order to receive a new token. Despite researching various examples provided by Cognit ...

What is the reason that the text-overflow property does not function properly within a nested flexbox

Why does my overflow text only show ellipsis when one parent element with display: flex is removed? It seems the text width determines the parent's width rather than using ellipsis. To test this behavior, try reducing the browser width in the provided ...

Content within Drawer featuring Material UI Scrollable Tabs

I am currently utilizing a material-ui drawer with the mini variant drawer structure. Inside this drawer, I have incorporated a series of tabs utilizing the prevent scroll buttons tabs design. The version of material-ui being used is 4.12.3. Regrettably, ...

Angular noticed a shift in the expression once it was verified

Whenever I try to invoke a service within the (change) action method, I encounter this issue: ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ng-untouched: true'. Cur ...

Is there a way for me to identify selected text within a textarea field?

Is there a way to specifically detect if text is highlighted within a textarea, without capturing text from other parts of the document that might be highlighted? I'm aware of using window.getSelection(), but I only want to target text within the text ...

How to efficiently pass props between components in NextJs

This is the project's file structure: components ├─homepage │ ├─index.jsx ├─location │ ├─index.jsx pages │ ├─location │ │ ├─[id].jsx │ ├─presentation │ │ ├─[id].jsx │ ├─_app.jsx │ ├─index.jsx ...

Methods for preventing a component from remounting when an update to the URI is necessary (such as for filter buttons)

With the use of react-router v3 and redux, my route setup looks something like this: <Route name="app" path="/" component={App}> <IndexRoute component={Catalogue} /> <Route name="brand" path="/brand/(:brand)" component={Catalogue} /> ...

When a user chooses a file name from a DropDown-List control, automatically direct them to a new page

Here is the code in .aspx: <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem value="">About Us</asp ...