/    Sign up×
Community /Pin to ProfileBookmark

cell color switch

HI ?

I am pretty new to javascript and I Have been beating my head agianst the wall for days now.

I need help.

What I want is to have a cell so when you click on it, it switches to a color. But heres the catch I have a list of colors and every time you click the cell I would like it to switch to the next color on the list then when you at the end of the color list it repeats it’s self.

For example if I have cells 1 -4

and colors 1-5

I click on cell 1 once I should get the 1st color
I click on cell 2 I should get the second color
I click back on cell 1 I should get the third color

I don’t think this will help but heres my code:

[COLOR=darkblue] <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>

<html>
<script language = “JavaScript” type = “text/javascript”>
var BGC;
var COL;
function add(a)
{
var x = x+1;
}
if (x = 1){BGC=”white”; COL = “black”;}
else if (x = 2){BGC=”black”; COL = “white”;}
else if (x = 3){BGC=”yellow”; COL = “green”;}
else if (x = 4){BGC=”green”; COL = “yellow”;}
else if (x = 5){BGC=”red”; COL = “blue”;}
else if (x = 6){BGC=”blue”; COL = “red”;}
else if (x = 7){BGC=”black”; COL = “orange”;}
else if (x = 8){BGC=”white”; COL = “orange”;}
else if (x = 9){BGC=”yellow”; COL = “blue”;}
else if (x >=10){x=1;}
</script>
<head>
<title>TITLE</title>
</head>

<body>
<table>
<tr>
<td bgcolor = BGC; color = COL; onclick =”add(‘a’)”; id= “yellow” >yellow</td>
<td id = “hello” onclick=”alert(‘click’);”>hello</td>
</tr>
<tr>
<td id = “mellow” onclick =”colorchange(x);”>mellow</td>
<td id = “jello”>jello</td>
</tr>
</table>
<table border=1>
<tr><td onclick=”this.bgcolor=BGC” >A</td><td onclick=”this.bgColor=’red'”>C</td></tr>
<tr><td onclick=”this.bgColor=’red'”>B</td><td onclick=”this.bgColor=’red'”>D</td></tr>
</table>
</body>
</html> [/COLOR]

Thanks

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@oleragJan 06.2004 — Very primitive, but its an example...

[code=php]
<html>
<head>
<script type="text/javascript">
var fColor = new Array("black","white","green", "yellow","blue","red", "orange","orange","blue");
var bColor = new Array("white","black","yellow","green", "red", "blue","black", "white", "yellow");
var ctr = 1;

function setColor(cell) {
cell.style.backgroundColor=(bColor[ctr]);
cell.style.color=(fColor[ctr]);
if (ctr == 8)
ctr = 0;
else
ctr++;
}
</script>
</head>
<body>
<table border="1">
<tr>
<td onClick="setColor(this)">
Row 1 / 1
</td>
<td onClick="setColor(this)">
Row 1 / 2
</td>
<td onClick="setColor(this)">
Row 1 / 3
</td>
</tr>
<tr>
<td onClick="setColor(this)">
Row 2 / 1
</td>
<td onClick="setColor(this)">
Row 2 / 2
</td>
<td onClick="setColor(this)">
Row 2 / 3
</td>
</tr>
</table>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@NatdripauthorJan 06.2004 — Thanks olerag

Is there a way I can do it without using arrays?

And if so can you show me? ?
Copy linkTweet thisAlerts:
@furious70Jan 06.2004 — why wouldn't you want to use arrays? Seems like the most logical choice to me, 2 global arrays and 1 global counter, since you want one list for all the cells.
Copy linkTweet thisAlerts:
@oleragJan 06.2004 — Well I only used the array (actually, two) to store the

colors you had. If you prefer your way of running thru a

if/else "switch", have at it.

Really, mine is not a great example. The main intent was to

show availability to the cell.style.backgroundColor() and

cell.style.color() functions, used to alter the color

presentation of cells.

Khalid Ali's site (that he will sometimes place links to that

provide examples) have nice rendering for tables (mind you

its some "heavy" JS with CSS).
Copy linkTweet thisAlerts:
@PittimannJan 06.2004 — Hi!

I also wonder very much, why you want to avoid arrays.

olerag's code is absolutely ok and fine with the arrays.

Well - seems, you insist. This code will deal with the first cell WITHOUT using arrays (and it's a stupid code, at least because of that):
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
&lt;html&gt;
&lt;script language = "JavaScript" type = "text/javascript"&gt;
var x=0;
function add(iddi){
x++;
if (x == 1){document.getElementById(iddi).style.backgroundColor="white"; document.getElementById(iddi).style.color = "black";}
else if (x == 2){document.getElementById(iddi).style.backgroundColor="black"; document.getElementById(iddi).style.color = "white";}
else if (x == 3){document.getElementById(iddi).style.backgroundColor="yellow"; document.getElementById(iddi).style.color = "green";}
else if (x == 4){document.getElementById(iddi).style.backgroundColor="green"; document.getElementById(iddi).style.color = "yellow";}
else if (x == 5){document.getElementById(iddi).style.backgroundColor="red"; document.getElementById(iddi).style.color = "blue";}
else if (x == 6){document.getElementById(iddi).style.backgroundColor="blue"; document.getElementById(iddi).style.color = "red";}
else if (x == 7){document.getElementById(iddi).style.backgroundColor="black"; document.getElementById(iddi).style.color = "orange";}
else if (x == 8){document.getElementById(iddi).style.backgroundColor="white"; document.getElementById(iddi).style.color = "orange";}
else if (x == 9){document.getElementById(iddi).style.backgroundColor="yellow"; document.getElementById(iddi).style.color = "blue";}
else{x=0;add(iddi);}
}
&lt;/script&gt;
&lt;head&gt;
&lt;title&gt;TITLE&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td onclick ="add(this.id)"; id= "yellow" style="background-color: yellow"&gt;yellow&lt;/td&gt;
&lt;td id = "hello" onclick="alert('click');"&gt;hello&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

Cheers - Pit
Copy linkTweet thisAlerts:
@NatdripauthorJan 06.2004 — Hi

I am not trying to knock the work that you have shown me with arrays.

I am just wanting to understand it in a different light. what I really want is to understand it with my current knowledge and currently my knowledge (until now) did not cover arrays.

? Thank you Very much Pit.


Thanks
Copy linkTweet thisAlerts:
@PittimannJan 06.2004 — Hi!

Why not take advantage of olerag's code?

You'd start learning a bit about arrays ?...

Cheers - Pit
Copy linkTweet thisAlerts:
@NatdripauthorJan 06.2004 — I most definatly am.. ?

Thanks Everyone for your help
×

Success!

Help @Natdrip spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...