/    Sign up×
Community /Pin to ProfileBookmark

help with changing cell property in javascript

I’m am trying to get this script to work. I want to change background colors of the cell. But really I’m a noob to javascript and I don’t understand what I’m doing wrong. please just give me some hints towards making it right, So I can learn better.

code:

[CODE]<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta content=”text/html; charset=ISO-8859-1″ http-equiv=”content-type”>
<title>Javascript can be so confusing</title>
<style type=”text/css”>
.text
{
font: 10pt arial;
color: black;
}
</style>
<script type=”text/javascript”>

function changeBG(id, whichColor) {

var backColor = [‘BLACK’, ‘WHITE’, ‘SILVER’, ‘PINK’, ‘RED’, ‘GOLD’, ‘GREEN’, ‘PURPLE’, ‘BLUE’, ‘NAVY’, ‘ORANGE’, ‘TURQUOISE’];
var aryColor= new array(12);

aryColor[0] = “BLACK”;
aryColor[1] = “WHITE”;
aryColor[2] = “SILVER”;
aryColor[3] = “PINK”;
aryColor[4] = “RED”;
aryColor[5] = “GOLD”;
aryColor[6] = “GREEN”;
aryColor[7]= “PURPLE”;
aryColor[8] = “BLUE”;
aryColor[9] = “NAVY”;
aryColor[10] = “ORANGE”;
aryColor[11] = “TURQUOISE”;

for (i=0; i < aryColor.length; i++) {
var preload = new backColor[];
preload.src = aryColor[i];
}
document.getElementById(id).style.backgroundColor = backColor.aryColor[whichColor];
return false;
}

</script>
</head>
<body>
<table cellpadding=”0″ cellspacing=”0″ align=”center” valign=”center” style=”width:500px; background-color:#F5F4E0; margin:0 auto; padding-bottom:20px;”>
<tr>
<td>
<table border=0 cellpadding=”0″ cellspacing=”3″ align=”center” valign=”center”>
<tr>
<td valign=”top” style=”text-align:center;”>
<p><span class=”text”><b>Will change the background color of the cell below!</b></span><br><br>
<form>
<select name=’Border’ class=’select’ onchange=”return changeBG(‘focus’,’arycolor’);”>
<option value=’BLACK’>Black </option>
<option value=’WHITE’>White </option>
<option value=’SILVER’>Silver </option>
<option value=’PINK’>Pink </option>
<option value=’RED’>Red </option>
<option value=’GOLD’>Gold </option>
<option value=’GREEN’>Green </option>
<option value=’PURPLE’>Purple </option>
<option value=’BLUE’>Blue </option>
<option value=’NAVY’>Navy Blue </option>
<option value=’ORANGE’>Orange </option>
<option value=’TURQUOISE’>Turquiose </option>

</select> </form>
<br>

</p>
</td>

</tr>
</table> <td>
<table id=”focus” align=”center” style=”height:100px; width:100px; border:3px double black; margin:0 auto;”>
<tr><td></td></tr>
</table>
</td> </tr>
</td>
</tr>
</table>
</body>
</html>[/CODE]

Thanks in advance, Happy Holidays

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@TileArtworksauthorDec 25.2008 — This is my original which changes the cell color to black.

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>Javascript can be so confusing</title>
<style type="text/css">
.text
{
font: 10pt arial;
color: black;
}
</style>
<script type="text/javascript">

function changeBG(id, whichColor) {
var backColor = ['BLACK', 'WHITE', 'SILVER', 'PINK', 'RED', 'GOLD', 'GREEN', 'PURPLE', 'BLUE', 'NAVY', 'ORANGE', 'TURQUOISE'];
document.getElementById(id).style.backgroundColor = backColor[whichColor];
return false;

}
</script>
</head>
<body>
<table cellpadding="0" cellspacing="0" align="center" valign="center" style="width:500px; background-color:#F5F4E0; margin:0 auto; padding-bottom:20px;">
<tr>
<td>
<table border=0 cellpadding="0" cellspacing="3" align="center" valign="center">
<tr>
<td valign="top" style="text-align:center;">
<p><span class="text"><b>Will change the background color of the cell below!</b></span><br><br>
<form>
<select name='Border' class='select' onchange="return changeBG('focus','0');">
<option value='BLACK'>Black </option>
<option value='WHITE'>White </option>
<option value='SILVER'>Silver </option>
<option value='PINK'>Pink </option>
<option value='RED'>Red </option>
<option value='GOLD'>Gold </option>
<option value='GREEN'>Green </option>
<option value='PURPLE'>Purple </option>
<option value='BLUE'>Blue </option>
<option value='NAVY'>Navy Blue </option>
<option value='ORANGE'>Orange </option>
<option value='TURQUOISE'>Turquiose </option>

</select> </form>
<br>

</p>
</td>

</tr>
</table> <td>
<table id="focus" align="center" style="height:100px; width:100px; border:3px double black; margin:0 auto;">
<tr><td></td></tr>
</table>
</td> </tr>
</td>
</tr>
</table>
</body>
</html>[/CODE]


I know it changes to black because the the [B]Forms[/B] select name says:
[CODE]<select name='Border' class='select' onchange="return changeBG('focus','0');">[/CODE]
but there is more is something needed like an array...
Copy linkTweet thisAlerts:
@mikemDec 26.2008 — Hi, Select elements adopt the value of the currently selected option, so you can pass its value to the script along with the id of the target element to set its background color. Something like this...[CODE]function changeBG(id, color)
{
document.getElementById(id).style.background = color;
}[/CODE]
[code=html]<select name='Border' class='select' onchange="changeBG('focus', this.value)">[/code]You can also change the values to hexadecimal for unrecognized color names. Mike ?
Copy linkTweet thisAlerts:
@TileArtworksauthorDec 26.2008 — this javascript is blowing my mind.... Anyone who has better learning site on the web, it'll be great if you could send me links... thanks mikem i'll be around helping(as much as i can) and pestering... : )

happy holidays
Copy linkTweet thisAlerts:
@mikemDec 26.2008 — Selam (Hi) TileArtworks. Bir &#351;ey de&#287;il. (You're welcome.) Mike ?
×

Success!

Help @TileArtworks 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 6.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...