1

I have an foreach echo, which displays a list of items on my website and I want to make a system to add a green background (when a checkbox is checked) and remove that checkbox when its no more checked.

echo '<form action="selector.php" id="form1" method="post" target="_self">';

foreach ($Query as $row) {  
  echo '
    <div class="item">
      <img src="something.png">
                            
      <label class="containerPen" style="color: #00ff00">
        Accept                              
        <input type="checkbox"  name="accepted[]" value="'.$row['topic_identification'].'" id="accepted"></input>
        </br>
        <span class="checkmarkPen"></span>
      </label>
                        
      <label class="containerPenX" style="color: #ff0000">
        Decline
        <input type="checkbox" name="declined[]" value="'.$row['topic_identification'].'" id="declined"></input>
        <span class="checkmarkPenX"></span>
      </label>
    </div>          
  ';
}

echo '
  <input type="submit" name="submit" value="submit"> 

  </form>
';

For each checkbox, I want to add the class

.greenbox {
  background-color: green;
}

when it's checked, and when I uncheck it, I want it to remove the class.

Thank you!

Here is the CSS For the class ContainerPen and CheckmarkPen

    .containerPen {
  display: block;
  position: relative;
  padding-left: 35px;
  margin-bottom: 12px;
  cursor: pointer;
  font-size: 22px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}


.containerPen input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}


.checkmarkPen {
  position: absolute;
  top: 0;
  left: 0;
  height: 25px;
  width: 25px;
  background-color: #eee;
}


.containerPen:hover input ~ .checkmarkPen {
  background-color: #00ff00;
}


.containerPen input:checked ~ .checkmarkPen {
  background-color: #00ff00;

}


.checkmarkPen:after {
  content: "";
  position: absolute;
  display: none;
}


.containerPen input:checked ~ .checkmarkPen:after {
  display: block;
}


.containerPen .checkmarkPen:after {
  left: 9px;
  top: 5px;
  width: 7px;
  height: 12px;
  border: solid white;
  border-width: 0 3px 3px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

rew
  • 29
  • 6

3 Answers3

2

As an alternative method;

Depending on which browsers you are supporting, you can simply do what you're trying to do through the CSS :checked pseudo-class selector.

You can read up on it here and see compatibility here.

Usage example would be;

input:checked
{
    background-color: green;
}

Or something like;

label input:checked
{
    background-color: green;
}

If you only want to have it affect inputs inside labels. Note that this selector will also apply to radio buttons or options inside selects.

Alain Doe
  • 532
  • 3
  • 7
  • None of them are working... I forgot to add the CSS For the label & span. I edited the main post. I would really love to solve this with CSS but as far I tried it didn't worked. :C – rew Jun 03 '21 at 09:46
  • 1
    I copied the HTML and CSS you provided in your post to [this](https://jsfiddle.net/JustCodingHereAndThere/0n7849h5/) JSFiddle and the Accept "checkbox" seems to color green when it is clicked. What is your expected result exactly? – Alain Doe Jun 03 '21 at 10:03
  • https://imgur.com/a/XXdeX51 I need the label to become green, not the checkbox. I added the CSS into the main post. – rew Jun 03 '21 at 10:06
  • 1
    So you want the *parent element* of the checkbox to have the green background and not the checkbox itself? In that case... with your current HTML set up that is impossible through CSS at the moment as you can't select a parent or previous element through CSS selectors. See [this](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector/1014958#1014958) page for some more information. It looks like JavaScript is your only solution at the moment. – Alain Doe Jun 03 '21 at 10:18
0
$onload = <<<EOT
 <script>    
    window.onload = function() {
       const chk = document.querySelectorAll('input[type="checkbox"]');
       for(let x=0; x< chk.length; x++) {
        chk[x].addEventListener('change', function (e) {
          if (e.target.checked) e.target.classList.add('greenbox');
          else e.target.classList.remove('greenbox')
       })
      }
    }
</script>

EOT;

// then add it where you want.

echo $onload;

Important This kind of echo is called Heredoc and has 2 requirements (or it will error out). There can be NO spaces after the initial <<<EOT (just a line break), and there can be no spaces before the closing EOT;. In fact, the last line can only be EOT; Just make sure about the spaces. Otherwise, it's a great way to form a readable php output for things like html and javascript blocks.

Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • I tried using this but I didn't understand how to use the $onload =<< – rew Jun 03 '21 at 09:51
  • Yeah, heredoc is finicky. I updated my answer with a note about usage. You just need to make a small adjustment – Kinglish Jun 03 '21 at 14:01
0

you can either add an eventListener or an onclick event on both checkboxes that pass the id to the function.

Then you can use JavaScript's built-in classlist properties to add and remove class.

const addClass = (id) => {
    let checkbox = document.querySelector(`#${id}`);
    checkbox.classList.toggle('greenbox');
}
.greenbox{
  background-color: green;
}
<input type="checkbox" onclick="addClass(this.id)" id="accepted">    
<input type="checkbox" onclick="addClass(this.id)" id="declined">
ahsan
  • 1,409
  • 8
  • 11
  • Hello! I tried this and it's adding the greenbox to the input, I need it to be added to the label. I tried to move the onclick function to the label and nothing is happening... – rew Jun 03 '21 at 09:49
  • 1
    In case of Label use "checkbox.parentElement.classList.toggle('greenbox')" this applies the class on the parent Element of input, which in your case is Label – ahsan Jun 03 '21 at 09:52
  • Working, but not correctly. When I check any checkbox, it's taking only the first one https://imgur.com/a/XXdeX51 – rew Jun 03 '21 at 09:57
  • That might be because you are possibly reusing element `id`s. They are supposed to be unique. – Alain Doe Jun 03 '21 at 10:19