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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    square that changes color when clicked

    Hi!!

    I need a simple javascript/ajax code to make a simple square change color (about 5 different colors) every time you click without refreshing the page.

    I just have the css of the square.
    <style>
    #square{position:absolute; width:50px; height:50px; background-color:#000000;}
    </style>

    Appreciate any help

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    <body>
    <div onclick="changeCol(this)" id="square"></div>
    <script type="text/javascript">
    var count=0;
    function changeCol(thediv){
    var colors=["red", "blue","green","yellow","orange"]
    thediv.style.backgroundColor=colors[count++%colors.length]
    }
    </script>
    </body>

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much!!!!
    works perfectly


  •  

    Posting Permissions

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