0
<div class="content">
          <?php
            for ($i=0; $i < 5; $i++) {
              echo '<div class="foo">
              </div>';
            }
          ?>
          <script>
            var foo= document.getElementsByClassName('foo');
            for (var i = 0; i < foo.length; i++) {
              foo[i].addEventListener("click", function(){
                this.style.backgroundColor = "lightgreen";
                });
            }
          </script>

I have created one div class called "content" and inside that content div there are five divs generated using php for loop. I want to change the divs "foo" background color to light green which is achieved already, but my concern is how to change the background color property again to white?

3
  • 2
    Maybe toggle a class instead Commented Aug 24, 2018 at 5:24
  • Welcome to SO, Please add your code into snippet, So someone has quickly fixed your issue. Commented Aug 24, 2018 at 5:24
  • Create a class with background-color: lightgreen and toggle this class on the element instead: this.classList.toggle('your_class_name'). Commented Aug 24, 2018 at 5:27

3 Answers 3

1

Extending the Lahiru TM approach

Try using this, so that it will toggle the color on click

<div class="content">
     <?php
       for ($i=0; $i < 5; $i++) {
          echo '<div class="foo">test
           </div>';
       }
     ?>
</div>
<script>
var foo= document.getElementsByClassName('foo');
for (var i = 0; i < foo.length; i++) {
    foo[i].addEventListener("click", function(e){
        if (this.style.backgroundColor == 'lightgreen') {
            this.style.backgroundColor = "white";
        } else {
            this.style.backgroundColor = "lightgreen";
        }
    });
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

You can try below approach for that,

<script>
var foo= document.getElementsByClassName('foo');
for (var i = 0; i < foo.length; i++) {
    foo[i].addEventListener("click", function(e){
        if (e.style.backgroundColor) {
            e.style.backgroundColor = "null";
        } else {
            e.style.backgroundColor = "lightgreen";
        }
    });
}
</script>

Comments

0

It is advisable to use the CSS class then of a class property.

You can do something like this

    <div class="content">
         <?php
           for ($i=0; $i < 5; $i++) {
              echo '<div id="i" class="preToggle" (click)="divClicked($event , i)">test
               </div>';
           }
         ?>
    </div>
    <script>
    divClicked(event , i){
        var foo= document.getElementById(i);
        foo.className = foo.className.replace("preToggle", "postToggle");
}
    </script>

define CSS for preToggle and postToggle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.