Sometimes, checking a checkbox will trigger a cascading effect on other checkboxes

I have implemented 5 checkboxes in a vertical layout, each created using SVG and Polyline elements. The toggling functionality works fine when clicking on the label of each checkbox. However, sometimes clicking on the checkbox image toggles the wrong box!

.cbx {
  // CSS code for styling checkboxes
}
// More CSS code to style checkboxes

The HTML structure:

<div class="ax-cb-div">
  <input class="inp-cbx" id="1" type="checkbox" style="display: none;"/>
  <label for="1" class="cbx"><span>
    // Checkbox 1 content here
  </label>
</div>

// Repeat above structure for checkboxes 2, 3, and 4

After selecting checkboxes 1, 2, 3, and 4, if you try to uncheck checkbox 3 by clicking on it, checkbox 4 may get unchecked instead. Clicking again on checkbox 3 will uncheck it properly.

Visit this CodePen link for reference.

Answer №1

By applying transform: scale(3.5) to the checkbox, it increases in size and ends up covering other checkboxes.

.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(1); // Adjusting the scale back to its original size instead of 3.5
  opacity: 0;
  transition: all 0.6s ease;
}

.cbx {
  margin: auto;
  margin-top: 2px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #FFFFFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transition-delay: 0.1s;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #506EEC;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 50%;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #506EEC;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #506EEC;
  border-color: #506EEC;
  animation: wave 0.4s ease;
}
.inp-cbx:checked + .cbx span:first-child svg {
  stroke-dashoffset: 0;
}
.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(1);
  opacity: 0;
  transition: all 0.6s ease;
}

@keyframes wave {
  50% {
    transform: scale(0.9);
  }
}
<div class="ax-cb-div">
  <input class="inp-cbx" id="1" type="checkbox" style="display: none;"/>
  <label for="1" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 1</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="2" type="checkbox" style="display: none;"/>
  <label for="2" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 2</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="3" type="checkbox" style="display: none;"/>
  <label for="3" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 3</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="4" type="checkbox" style="display: none;"/>
  <label for="4" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 4</span>
  </label>
</div>

Answer №3

Make sure to utilize the value attribute.

<input id="1" value="example" ../>
<input id="3" value="sample" ../>

Check out MDN Web docs - Input

Answer №4

Adjust the transform property value to 1.5

.inp-cbx:checked + .cbx span:first-child:before {
   transform: scale(1.5);
   opacity: 0;
   transition: all 0.6s ease;
}

Answer №5

Include pointer-events: none; in the style of .cbx span:first-child:before. The issue is caused by the animation.

In the .cbx span:first-child:before section, we are transitioning the content:"" from scale(0) and opacity:1 to scale(3.5) and opacity:0 respectively. Essentially, we are only hiding that object with opacity, meaning the object will still be there and clickable. This is why it's necessary to add pointer-events:none to the .cbx span:first-child:before style.

To test this, simply select the 4th checkbox and examine it using the

.inp-cbx:checked + . 
cbx span:first-child:before
style. Change the opacity from 0 to opacity:1. You will see something like what is shown below.

https://i.sstatic.net/GzeEN.png

In the image above, you can observe that the span:before of the 4th checkbox covers the 3rd checkbox and remains clickable. So, when you click on the 3rd checkbox, it actually registers as a click on the 4th one.

Explore the code snippet provided below:

.cbx {
  margin: auto;
  margin-top: 2px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.cbx span {
  display: inline-block;
  vertical-align: middle;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 3px;
  transform: scale(1);
  vertical-align: middle;
  border: 1px solid #9098A9;
  transition: all 0.2s ease;
}
.cbx span:first-child svg {
  position: absolute;
  top: 3px;
  left: 2px;
  fill: none;
  stroke: #FFFFFF;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 16px;
  stroke-dashoffset: 16px;
  transition: all 0.3s ease;
  transition-delay: 0.1s;
  transform: translate3d(0, 0, 0);
}
.cbx span:first-child:before {
  content: "";
  width: 100%;
  height: 100%;
  background: #506EEC;
  display: block;
  transform: scale(0);
  opacity: 1;
  border-radius: 50%;
  pointer-events:none;
}
.cbx span:last-child {
  padding-left: 8px;
}
.cbx:hover span:first-child {
  border-color: #506EEC;
}

.inp-cbx:checked + .cbx span:first-child {
  background: #506EEC;
  border-color: #506EEC;
  animation: wave 0.4s ease;
}
.inp-cbx:checked + .cbx span:first-child svg {
  stroke-dashoffset: 0;
}
.inp-cbx:checked + .cbx span:first-child:before {
  transform: scale(3.5);
  opacity: 0;
  transition: all 0.6s ease;
}

@keyframes wave {
  50% {
    transform: scale(0.9);
  }
}
<div class="ax-cb-div">
  <input class="inp-cbx" id="1" type="checkbox" style="display: none;"/>
  <label for="1" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 1</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="2" type="checkbox" style="display: none;"/>
  <label for="2" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 2</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="3" type="checkbox" style="display: none;"/>
  <label for="3" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 3</span>
  </label>
</div>

<div class="ax-cb-div">
  <input class="inp-cbx" id="4" type="checkbox" style="display: none;"/>
  <label for="4" class="cbx"><span>
    <svg width="12px" height="10px" viewbox="0 0 12 10">
      <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
    </svg></span><span>Checkbox number 4</span>
  </label>
</div>

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

How to center a video horizontally using Bootstrap styling

In my search for a solution, I have not been able to find a way to horizontally center a video within a Bootstrap div or header section while also keeping the video vertically aligned with the top. The div or header section has a 100% width with fixed heig ...

Is there a Pug file that can connect to a Websocket and show the incoming values?

Check out this basic pug file snippet: p Hello The back end consists of typical components such as node, express, passport, and sql. You can set up a route like this: app.get('/example', (request, response) => { response.render('exam ...

Leveraging route configuration's scope in HTML

As a beginner in AngularJs, I am currently exploring the creation of a single page application. However, I am encountering difficulties in converting my initial code into more professional and efficient code. During this conversion process, I have separate ...

Adjust the height of an element using percentages

Over at this JSFiddle link, they demonstrated splitting a page into 3 parts. However, when I adjust the width of #wrapper to 100%, everything works perfectly. So why does changing height:400px to height:100% cause it to fail? #wrapper { width: 400px; ...

Directive isolated scope is encountering an undefined $scope variable

After searching through various Stack Overflow posts on the issue, I have tried different solutions but still can't get my directive to render. The goal is to pass an integer to my directive within an ng-repeat loop and display an emoji based on that ...

Unable to modify the appearance of an HTML element when injected via a Chrome extension

I am currently developing a unique chrome extension that uses Ajax to inject custom HTML into the current tab. This extension appends a <div> element to the body, and now I need to manipulate it using JavaScript. Specifically, I want it to dynamical ...

issues arise with the child's height reaching 100%

I am experiencing some height issues with the div.wrapper .content ul.feeder element in my code. Can anyone help me figure out what's going on? Here is a snippet of the code I am working with: html, body {height:100%;background:#F7F7F7;} * { mar ...

Stubborn boolean logic that refuses to work together

Seeking guidance on resolving a persistent issue with my website that has been causing me headaches for the past few weeks. This website was created as my capstone project during a recent graduate program, where I unfortunately did not achieve all the desi ...

What is the best way to convert a repetitive string into a reusable function?

I am currently retrieving data from an API and I want to display it on my website in a more user-friendly manner. The challenge I'm facing is that the number of variables I need is constantly changing, along with their corresponding values. So, I&apos ...

The data type "100%" cannot be assigned to a number type in the kendo-grid-column

I need the columns in the grid to have a width of 100%. <kendo-grid-column field="id" title="id" [width]="'100%'"> </kendo-grid-column> However, when I hover over the code, I get this message: The value "100%" cannot be ...

How can PHP send a variety of error messages to an Ajax/Jquery script and display them in an HTML DIV?

In an attempt to send various error messages back to an HTML DIV based on the PHP outcome, I have scoured through different resources without a solution that fits my particular case. Hopefully, someone can provide assistance with this. The scenario involv ...

Adjust the height of a div element and enable scrolling to fit the

There are numerous inquiries regarding this particular issue. I have exhausted nearly all possible solutions I could find, yet none of them delivered the desired outcome. This is the structure of my page: <div> <header id="status"> / ...

Here is a helpful guide on updating dropdown values in real time by retrieving data from an SQL database

This feature allows users to select a package category from a dropdown menu. For example, selecting "Unifi" will display only Unifi packages, while selecting "Streamyx" will show only Streamyx packages. However, if I first select Unifi and then change to S ...

How can I add a drop-down list to a cell within a table?

Can a drop-down list be added into a table cell? Thank you! Here is an excerpt of the code: $sql_query="SELECT nameOfwarning FROM warnings"; $sodss=mysqli_query($d,$sql_query); if (mysqli_num_rows($result)<1) ?> <select name = "warnings"> ...

Can an iPhone be enabled to detect other devices within the same local network?

If you have five devices connected to the same local network, can iOS device 1 function as a central hub that "discovers" the other four devices on the network and communicates with them in a server-like manner? Would establishing communication through a ...

execute the changePage() function in jQuery using the data stored in localStorage

In my jQuery mobile page, I have a simple setup: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.2"> <link rel="stylesheet" href="js/jquery.mobile-1.2.0.css" /> < ...

The menu isn't displaying properly and the onclick function seems to be malfunctioning

My onclick event is acting strange. Whenever I click the mobile menu in the menubar, it just appears briefly like a flash and then disappears. It's not staying stable on the screen. The classes are being added and removed abruptly when I try to click ...

Is there a method to keep the color of navigation links active even when the cursor is not directly over the element?

In my navbar, I'm facing an issue where the color of the nav links returns to white when the hover cursor leaves the element. Is there a way to keep the color of the nav links even when the cursor is not on the hover elements? To illustrate, I've ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

What CSS property can be used to make short words wrap to the next line?

Is it possible to automatically identify if a line of text ends with a short word and then move it to the next line, so that the text flows smoothly? This would ensure a more organized layout. For instance, I envision the following text: Cascading Style ...