Skip to content Skip to sidebar Skip to footer

Remove The Javascript From Toggling A Button

Here is the code that currently toggles a button. I would like to remove the javascript component of it by maybe using a checkbox. The only requirement I have is that I have to use

Solution 1:

As mentioned in your previous post, it cannot be achieved with only a checkbox because a checkbox cannot have content, and thus, no pseudo elements ::before or ::after, since both operate on content.

.togglerCheckbox {
  display: none; }

.toggler {
  width: 140px;
  height: 40px;
  background-color: #B3B3B3;
  color: #FFF;
  clear: both;
  border-radius: 5px;
  font-size: 12px;
  /* new styles here: */
  padding: 0 0 0 24px;
  box-sizing: border-box;
  display: inline-block;
  position: relative;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  transition: padding .25s ease;
}

.togglerCheckbox:checked + .toggler {
  padding: 0 24px 0 0;
}

.toggler:before {
  content: '';
  display: block;
  width: 20px;
  height: 36px;
  background-color: #4D4D4D;
  position: absolute;
  left: 2px;
  top: 2px;
  border-radius: 5px;
  /* transition to make it look smoother */
  transition: left .4s ease;
}

.togglerCheckbox:checked + .toggler:before {
  left: calc(100% - 22px);
}
<input type="checkbox" id="mycheckbox" class="togglerCheckbox" /><label for="mycheckbox" class="toggler">Text Value</label>

Solution 2:

To achieve expected result, you can try below option CSS:

#toggle:checked + .toggler:before{
  left: calc(100% - 22px);
}

HTML:

<input type="checkbox" name="toggle" id="toggle" />
<div class="toggler">Text Value</div>

https://codepen.io/nagasai/pen/aqMbym


Post a Comment for "Remove The Javascript From Toggling A Button"