Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-17-2012, 04:47 PM   PM User | #1
LearningJS
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
LearningJS is an unknown quantity at this point
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
LearningJS is offline   Reply With Quote
Old 10-17-2012, 05:26 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Old 10-17-2012, 06:09 PM   PM User | #3
LearningJS
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
LearningJS is an unknown quantity at this point
Thank you so much!!!!
works perfectly
LearningJS is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.