n00b Q - Changing Background Color of Table Cells on Click
OK. I'm super new to any kind of java script. What I want is a menu that, when clicked, changes the background color of a table cell to blue. When a different option is clicked, the current highlighted table should turn back to gray and the new selection should be blue.
Well, I have all of that working. What I need now is just to have the first option start out highlighted and to become unhighlighted when another option is clicked.
Been a while since I properly touched javascript, but was browsing the unanswered questions and after reading, I think I can help.
Try changing the class of the first option to class="start". The start CSS definition is already set to navy, so that should work I think. Not a javascript fix, but should still work
TurMan I made some changes to your script. When you use onClick="select(this) you are sending [object HTMLDivElement] to your js function. Not sure if this is a good idea when you could use an ID. Add an alert to your function to see what it's working with.
Code:
function select(xx)
{
alert(xx);die;
if ( element ) {...
Since your doing this in a nav menu an ID for each <TD> is not out of the question for.
I'm sure you know most of your css is not used. So I just eliminated the unused and redid it a little bit. Here's what I came up with:
OK. I'm super new to any kind of java script. What I want is a menu that, when clicked, changes the background color of a table cell to blue. When a different option is clicked, the current highlighted table should turn back to gray and the new selection should be blue.
Do you want just the active menu tab to be highlighted when the menu tab is clicked?
I don't think he wants to actually change pages when a <td> is clicked on. Those pages show how to use a common CSS file to highlight the appropriate tab (or whatever) for the current page, but they don't show how to highlight tabs (<div>s, essentially) within a single page. Unless I don't understand them at all. (Certainly the second link is only for multiple pages. And interesting that they chose to do it with classes, I've done it with IDs, myself:
And interesting that they chose to do it with classes, I've done it with IDs, myself:
It makes no difference whether you use classes or id's. All that matters is that the selectors match which ever you use.
btw old pedant, if you copy and paste your code in post no. 6 and run it you'll get a lot of errors. I have made the syntax corrections to your code in red. There is still a logic error because td is undefined in which == td and which is an object anyway. I stopped checking for errors here.
Code:
<html>
<head>
<style type="text/css">
td.menu
{
background-color: gray;
text-align:center;
width: 188px;
color: white; /* ??? if this is the only reason you had the < a > tags */
}
</style>
<script type="text/javascript">
function select(which)
{ //function was misspelt
var tds = document.getElementById("layout").getElementsByTagName("td");
for ( var t = 0; t < tds.length; ++t )
{
if ( tds[t].className == "menu" )
{
//which is an object and td is undefined
tds[t].style.backgroundColor = ( which == td ) ? "navy" : "gray";
}
}
}
</script>
</head>
<body>
<table id="layout">
<tr>
<td class="menu" onclick="select(this);">Artists</td>
</tr>
<tr>
<td class="menu" onclick="select(this);">CPAs</td>
</tr>
<tr>
<td class="menu" onclick="select(this);">Doctors</td>
</tr>
<tr>
<td>This cell is not clickable and not affected</td>
</tr>
</table>
</body>
</html>
Last edited by webdev1958; 08-21-2011 at 10:45 AM..