Raydoan Anik
Raydoan Anik

Reputation: 219

change style of other labels when checkbox is checked

I have an issue with the checkbox checked. I am trying to find a solution that is when my current checkbox is checked to change the opacity others checkbox. is that possible with CSS? I made it with CSS input and label. Here is my code.

ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  margin-bottom: 10px;
  display: block;
}

.styled-checkbox {
  position: absolute;
  opacity: 0;
}
.styled-checkbox:checked + label {
  opacity: 1;
}
.styled-checkbox:checked + label label {
  opacity: 0.5;
}
.styled-checkbox + label {
  position: relative;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
}
.styled-checkbox + label:before {
  content: '';
  margin-right: 10px;
  display: inline-block;
  width: 14px;
  height: 14px;
  vertical-align: text-top;
  border: 1px solid #000;
}
.styled-checkbox:checked + label:after {
  content: '';
  position: absolute;
  left: 1px;
  top: 7px;
  background: white;
  width: 2px;
  height: 2px;
  box-shadow: 2px 0 0 #000, 4px 0 0 #000, 4px -2px 0 #000, 4px -4px 0 #000, 4px -6px 0 #000, 4px -8px 0 #000;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}
<ul>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-1" />
    <label for="styled-checkbox-1">Test one</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-2" />
    <label for="styled-checkbox-2">Test Two</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-3" />
    <label for="styled-checkbox-3">Test Three</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-4" />
    <label for="styled-checkbox-4">Test Four</label>
  </li>
</ul>

`

desire goal is something like that

enter image description here

I hope you guys understand the problem. Thanks in advance

Upvotes: 0

Views: 142

Answers (1)

Triby
Triby

Reputation: 1757

Just remove this declaration: .styled-checkbox:checked + label label and add another one .styled-checkbox.inactive + label to set opacity of "inactive" checkboxes.

  • After the DOM is loaded, get all checkboxes and add a click listener
  • If no checkbox is checked, remove 'inactive' class from all of them
  • If at least one checkbox is checked, add the 'inactive' class to all unchecked

window.addEventListener('load', function() {
    let checks = document.querySelectorAll('.styled-checkbox');
    checks.forEach(function(check) {
        check.addEventListener('click', chkStyles);
    });
    function chkStyles() {
        // Verify if there's at least one checkbox checked
        let checked = document.querySelectorAll('.styled-checkbox:checked');
        if(checked.length == 0) {
            // No checked checkbox, remove all inactive
            checks.forEach(function(check) { check.classList.remove('inactive'); });
        } else {
            // At least 1 is checked
            checks.forEach(function(check) {
                if(check.checked) {
                    check.classList.remove('inactive');
                } else {
                    check.classList.add('inactive');
                }
            });
        }
    }
});
ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

li {
  margin-bottom: 10px;
  display: block;
}

.styled-checkbox {
  position: absolute;
  opacity: 0;
}
.styled-checkbox:checked + label {
  opacity: 1;
}
.styled-checkbox.inactive + label {
  opacity: 0.5;
}
.styled-checkbox + label {
  position: relative;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  font-size: 0.875rem;
}
.styled-checkbox + label:before {
  content: '';
  margin-right: 10px;
  display: inline-block;
  width: 14px;
  height: 14px;
  vertical-align: text-top;
  border: 1px solid #000;
}
.styled-checkbox:checked + label:after {
  content: '';
  position: absolute;
  left: 1px;
  top: 7px;
  background: white;
  width: 2px;
  height: 2px;
  box-shadow: 2px 0 0 #000, 4px 0 0 #000, 4px -2px 0 #000, 4px -4px 0 #000, 4px -6px 0 #000, 4px -8px 0 #000;
  transform: rotate(45deg);
  transition: all 0.3s ease-out;
}
<ul>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-1" />
    <label for="styled-checkbox-1">Test one</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-2" />
    <label for="styled-checkbox-2">Test Two</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-3" />
    <label for="styled-checkbox-3">Test Three</label>
  </li>
  <li>
    <input type="checkbox" class="styled-checkbox" id="styled-checkbox-4" />
    <label for="styled-checkbox-4">Test Four</label>
  </li>
</ul>

Upvotes: 1

Related Questions