CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Ajax and Design (http://www.codingforums.com/forumdisplay.php?f=55)
-   -   square that changes color when clicked (http://www.codingforums.com/showthread.php?t=277414)

LearningJS 10-17-2012 04:47 PM

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:)

xelawho 10-17-2012 05:26 PM

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>


LearningJS 10-17-2012 06:09 PM

Thank you so much!!!!
works perfectly :)


All times are GMT +1. The time now is 01:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.