Fun with JavaScript!

Hoping someone can help me with this JavaScript problem. I’m trying to pass an array to a getElementById with the purpose of making multiple cells in the table take the class. I can get it working with one array location but not with more than one. Please help!

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<script>
function changecolors(redsarray,yellowsarray,greensarray,graysarray)
{

var redsarray = new Array();
redsarray[0]=’r1_c1′;
//redsarray[1]=’r1_c2′;

var yellowsarray = new Array();
yellowsarray[0]=’r2_c1′;
//yellowsarray[1]=’r2_c2′;

var greensarray = new Array();
greensarray[0]=’r3_c1′;
//greensarray[1]=’r3_c2′;

var graysarray = new Array();
graysarray[0]=’r4_c1′;
//graysarray[1]=’r4_c2′;

document.getElementById(redsarray).className=’red’;
document.getElementById(yellowsarray).className=’yellow’;
document.getElementById(greensarray).className=’green’;
document.getElementById(graysarray).className=’gray’;
}
</script>
<style type=”text/css”>
.red {background-color:red;}
.yellow {background-color:yellow;}
.green {background-color:green;}
.gray {background-color:gray;]
</style>
</head>
<body>
Content …
<table border=”1″>
<tr>
<td id=”r1_c1″>
r1_c1
</td>
<td id=”r1_c2″>
r1_c2
</td>
</tr>
<tr>
<td id=”r2_c1″>
r2_c1
</td>
<td id=”r2_c2″>
r2_c2
</td>
</tr>
<tr>
<td id=”r3_c1″>
r3_c1
</td>
<td id=”r3_c2″>
r3_c2
</td>
</tr>
<tr>
<td id=”r4_c1″>
r4_c1
</td>
<td id=”r4_c2″>
r4_c2
</td>
</table>

<button type=”button” onclick=”changecolors()”;>Button</button>

</body>

</html>

 

One thought on “Fun with JavaScript!”

  1. Hi my name is Sandra and I just wanted to drop you a quick note here instead of calling you. I discovered your Fun with JavaScript! | Hamish Taylor’s blog page and noticed you could have a lot more traffic. I have found that the key to running a successful website is making sure the visitors you are getting are interested in your niche. There is a company that you can get targeted visitors from and they let you try their service for free for 7 days. I managed to get over 300 targeted visitors to day to my site.

Leave a Reply

Your email address will not be published. Required fields are marked *