Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Dynamic Colors

  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post

    Dynamic Colors

    I want to have <div> cells that the background-color css style dynamically change between 4 colors depending on how many times they are clicked. The options are none, yellow, red and green then repeat. So when the page loads, they are all at none, and if you click once it goes to green, twice to yellow, three times to red, then back to none.

    I do not know if this is possible, but if anyone can point me to a tutorial, I would be appreciative. I just am having a hard time searching the proper terms.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    This is actually quite easy

    HTML:
    Code:
    <div class="bgchanger">Whatever the text may be</div>
    ...
    Javascript:
    Code:
    var myBgChangers = document.getElementsByClassName('bgchanger');
    for(i=0; i<myBgChangers.length; i++) {
       if(window.addEventListener) {
          myBgChangers[i].addEventListener('click', changeBgColor, false);
       } else {
          myBgChangers[i].attachEvent('onclick', changeBgColor);
       }
    }
    
    function changeBgColor() {
      // this function will be triggered each time one of the div's with class "bgchanger" is being clicked ... "this" is a reference to the current element
      switch(this.style.backgroundColor) {
        case "" : this.style.backgroundColor = "green"; break;
        case "green" : this.style.backgroundColor = "yellow"; break;
        case "yellow" : this.style.backgroundColor = "red"; break;
        case "red" : this.style.backgroundColor = ""; break;
      }
    }

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hmm, looks good, but I'm not getting the color changes...

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <
    html xmlns="http://www.w3.org/1999/xhtml">
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <
    title>Untitled Document</title>
    <
    script type="text/javascript" language="javascript">
    var 
    myBgChangers document.getElementsByClassName('bgchanger');
    for(
    i=0i<myBgChangers.lengthi++) {
       if(
    window.addEventListener) {
          
    myBgChangers[i].addEventListener('click'changeBgColorfalse);
       } else {
          
    myBgChangers[i].attachEvent('onclick'changeBgColor);
       }
    }

    function 
    changeBgColor() {
      
    // this function will be triggered each time one of the div's with class "bgchanger" is being clicked ... "this" is a reference to the current element
      
    switch(this.style.backgroundColor) {
        case 
    "" this.style.backgroundColor "green"; break;
        case 
    "green" this.style.backgroundColor "yellow"; break;
        case 
    "yellow" this.style.backgroundColor "red"; break;
        case 
    "red" this.style.backgroundColor ""; break;
      }
    }
    </script>
    </head>

    <body>
    <div class="bgchanger">Whatever the text may be</div>
    </body>
    </html> 
    Last edited by ziggy1621; 07-28-2011 at 05:35 PM.

  • #4
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by ziggy1621 View Post
    Hmm, looks good, but I'm not getting the color changes...
    ...because it had a few minor errors. This one works:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    
    var myBgChangers = document.getElementsByClassName('bgchanger');
    for(i=0; i<myBgChangers.length; i++) {
       if(window.addEventListener) {
          myBgChangers[i].addEventListener('click', changeBgColor, false);
       } else {
          myBgChangers[i].attachEvent('onclick', changeBgColor);
       }
    }
    
    function changeBgColor(a) {
      // this function will be triggered each time one of the div's with class "bgchanger" is being clicked ... "this" is a reference to the current element
      switch(a.style.backgroundColor) {
        case "" : a.style.backgroundColor = "green"; break;
        case "green" : a.style.backgroundColor = "yellow"; break;
        case "yellow" : a.style.backgroundColor = "red"; break;
        case "red" : a.style.backgroundColor = ""; break;
      }
    }
    </script>
    </head>
    
    <body>
    <div class="bgchanger" onclick="changeBgColor(this)">Whatever the text may be</div>
    </body>
    </html>
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    ziggy1621 (07-28-2011)

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thanks brother. I'm a PHP guy, so I've been spinning my head. Thanks aton!

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    The main reason was that you should have added my code to a window.onload event handler ... because you can only attach/add event listeners to existing elements.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •