How do I ignore onmouseover when onclick used on td cell?

Hi,

I'm just learning Javascript (and this DOM thing, which I guess is JScript) and try as I might, I can't figure out the following problem (and I've searched quite a bit).

I need to make my code recognize that a TD cell was clicked on, while also having the capability for the rest of the table to have onmouseover.

The onclick background color is lime.  Right now, I can't figure out how to disable the onmouseover JUST for the cell that I click on.  Any movement of my mouse cancels the onclick.

My question:  Is there a way to make the clicked cell stay lime until another cell in the table is clicked on?

My ideas have been:
1.  Use one (or several) If...else codes when determining to turn the background color off
2.  Tying into #1, I think I'd need to pass the cell's contents, when clicked, to a variable and then based on that variable, not allow that particular cell to change its background onmouseover until another cell gets clicked.

I don't know how to put this in action though.

I'm using IE7 and all the users for this page will be using it as well.  I've included the URL to the page in question if it helps.

Thanks for any help you can give!
<html><head>
<style type="text/css"> 
.regtext
{
    FONT: 16px Arial, Helvetica, sans-serif;
    COLOR: #000000
}
</style>
<script type="text/javascript">
 
function hiddenDiv() { 
document.getElementById('hideShow').style.display = 'none';
document.getElementById('hideShow2').style.display = 'none';
document.getElementById('hideShow3').style.display = 'none';
}
 
function showDiv() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideShow2').style.display = 'none';
document.getElementById('hideShow3').style.display = 'none';
document.getElementById('hideShow').style.display = 'block';
}
}
 
function showDiv2() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideShow').style.display = 'none';
document.getElementById('hideShow3').style.display = 'none';
document.getElementById('hideShow2').style.display = 'block'; 
} 
}
 
function showDiv3() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideShow').style.display = 'none';
document.getElementById('hideShow2').style.display = 'none';
document.getElementById('hideShow3').style.display = 'block'; 
} 
}
 
</script>
 
</head>
<body onload="javascript:hiddenDiv()">
<script>
function tdmouseover(el) {
el.style.color = "black"
el.style.backgroundColor = "orange"
}
 
function tdmouseout(el) {
el.style.color = "black"
el.style.backgroundColor = "white"
}
 
function tdmouseclickit(el) {
    el.style.color = "black"
    el.style.backgroundColor = "lime"
}
</script>
<br><br><br>
<table border=1 align=center width="45%" bordercolor=black cellpadding=3 width=95% cellspacing=0>
<tr height=25 class=regtext bgcolor=#ffffff>
<a href="javascript:showDiv()"><td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this)" align=center><b>Sun</td>
<a href="javascript:showDiv2()"><td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this)" align=center><b>Mon</td>
<a href="javascript:showDiv3()"><td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this)" align=center><b>Tue</td>
</tr>
</table>
<br><br><br><br><br>
<div align="center" class="regtext">
<div id="hideShow" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Sunday
</div>
<div id="hideShow2" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Monday
</div>
<div id="hideShow3" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Tuesday
</div>
</div>
</body>
</html>

Open in new window

Scootman78Asked:
Who is Participating?
 
ZvonkoSystems architectCommented:
It is possible:
<html>
<head>
<title>Zvonko &#42;</title>
<style type="text/css"> 
.regtext
{
    FONT: 16px Arial, Helvetica, sans-serif;
    COLOR: #000000
}
div.hideShow {
  display: none;
  margin-top:4px;
  font-weight:bold;
  text-decoration:underline;
}
 
</style>
<script >
 
var selDiv, selCell;
 
function tdmouseover(theCell) {
  if(theCell!=selCell){
    theCell.style.backgroundColor = "orange"
  }
}
 
function tdmouseout(theCell) {
  if(theCell!=selCell){
    theCell.style.backgroundColor = "white";
  }
}
 
function tdmouseclickit(theCell, divID) {
  if(selDiv){
    selDiv.style.display = 'none';
    selCell.style.backgroundColor = "white";
  }
  selDiv = document.getElementById(divID);
  selDiv.style.display = 'block';
  selCell = theCell;
  selCell.style.backgroundColor = "lime";
}
 
</script>
 
</head>
<body>
<br><br><br>
<table border=1 align=center width="45%" bordercolor=black cellpadding=3 width=95% cellspacing=0>
<tr height=25 class=regtext bgcolor=#ffffff>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow1')" align=center><b>Sun</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow2')" align=center><b>Mon</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow3')" align=center><b>Tue</td>
</tr>
</table>
<br><br><br><br><br>
<div align="center" class="regtext">
<div id="hideShow1" class="hideShow" > 
Sunday
</div>
<div id="hideShow2" class="hideShow" > 
Monday
</div>
<div id="hideShow3" class="hideShow" > 
Tuesday
</div>
</div>
</body>
</html>

Open in new window

0
 
ZvonkoSystems architectCommented:
Check this:
<html><head>
<style type="text/css"> 
.regtext
{
    FONT: 16px Arial, Helvetica, sans-serif;
    COLOR: #000000
}
#hideShow1 { display: none; }
#hideShow2 { display: none; }
#hideShow3 { display: none; }
 
</style>
<script >
 
var selDiv, selCell;
 
function tdmouseover(theCell) {
  if(!selDiv){
    theCell.style.backgroundColor = "orange"
  }
}
 
function tdmouseout(theCell) {
  if(!selDiv){
    theCell.style.backgroundColor = "white";
  }
}
 
function tdmouseclickit(theCell, divID) {
  if(selDiv){
    selDiv.style.display = 'none';
    selCell.style.backgroundColor = "white";
  }
  selDiv = document.getElementById(divID);
  selDiv.style.display = 'block';
  selCell = theCell;
  selCell.style.backgroundColor = "lime"
}
 
</script>
 
</head>
<body>
<br><br><br>
<table border=1 align=center width="45%" bordercolor=black cellpadding=3 width=95% cellspacing=0>
<tr height=25 class=regtext bgcolor=#ffffff>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow1')" align=center><b>Sun</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow2')" align=center><b>Mon</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow3')" align=center><b>Tue</td>
</tr>
</table>
<br><br><br><br><br>
<div align="center" class="regtext">
<div id="hideShow1" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Sunday
</div>
<div id="hideShow2" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Monday
</div>
<div id="hideShow3" style="margin-top:4px;font-weight:bold;text-decoration:underline;"> 
Tuesday
</div>
</div>
</body>
</html>

Open in new window

0
 
ZvonkoSystems architectCommented:
And here the Deselect feature:
<html>
<head>
<title>Zvonko &#42;</title>
<style type="text/css"> 
.regtext
{
    FONT: 16px Arial, Helvetica, sans-serif;
    COLOR: #000000
}
div.hideShow {
  display: none;
  margin-top:4px;
  font-weight:bold;
  text-decoration:underline;
}
 
</style>
<script >
 
var selDiv, selCell;
 
function tdmouseover(theCell) {
  if(!selDiv){
    theCell.style.backgroundColor = "orange"
  }
}
 
function tdmouseout(theCell) {
  if(!selDiv){
    theCell.style.backgroundColor = "white";
  }
}
 
function tdmouseclickit(theCell, divID) {
  if(selDiv){
    selDiv.style.display = 'none';
    selCell.style.backgroundColor = "white";
    selDiv = null;
  }
  if(selCell!=theCell){ 
    selDiv = document.getElementById(divID);
    selDiv.style.display = 'block';
    selCell = theCell;
    selCell.style.backgroundColor = "lime";
  }
}
 
</script>
 
</head>
<body>
<br><br><br>
<table border=1 align=center width="45%" bordercolor=black cellpadding=3 width=95% cellspacing=0>
<tr height=25 class=regtext bgcolor=#ffffff>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow1')" align=center><b>Sun</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow2')" align=center><b>Mon</td>
<td onmouseover="tdmouseover(this)" onmouseout="tdmouseout(this)" onclick="tdmouseclickit(this,'hideShow3')" align=center><b>Tue</td>
</tr>
</table>
<br><br><br><br><br>
<div align="center" class="regtext">
<div id="hideShow1" class="hideShow" > 
Sunday
</div>
<div id="hideShow2" class="hideShow" > 
Monday
</div>
<div id="hideShow3" class="hideShow" > 
Tuesday
</div>
</div>
</body>
</html>

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Scootman78Author Commented:
Zvonko, your first code is very very close to what I need.  The lime green background shows up on the onclick event, but the other cells no longer respond to the onmouseover event (orange background).

Your 2nd code wasn't what I was looking for though.  Sorry I forgot to mention this before, but once a cell is clicked, I don't want it to be clickable again until another cell is clicked.  In other words, if it has a lime green background, I'd like the cell to be disabled for taking any action until another cell is clicked and has a lime green background.

I don't know if any of this is possible.  I'm trying to create a calendar for a family website I'm putting together and I'm trying to reproduce results I would otherwise be able to get out of Access VBA (I wish my Javascript knowledge was just as good).
0
 
Scootman78Author Commented:
Awesome work!  I've been looking for weeks to try to get this to work.  Thanks very much!
0
 
mr_egyptianCommented:
Too late, but here's another option.  It's pretty much a total rewrite, and I don't know what your final intentions are, but here it is in case it's useful to you.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
  table{
	  		width: 45%;
			margin: auto;
  }
  
  td{
	  
  }
 
  td:hover{
	  		background-color: orange;
  }
  
  .regtext{
	  		font-family: Arial, Helvetica, sans-serif;
			font-size: 16px;
			font-weight: bold;
			text-align: center;
  }
  </style>
  <script type="text/javascript">
  <!--
   /* Array of td ids */
   var days = ['sun', 'mon', 'tue'];
   
   function toggle(el){
	   /* Highlight clicked cell */
	   el.style.backgroundColor = '#00ff00';
	   /* De-highlight other cells  */
	   for(i = 0; i < days.length; i++){
		   if(days[i] != el.id){
				   document.getElementById(days[i]).style.backgroundColor='';
		   }
	   }
	   /* Set 'hidden' div contents for correct day */
	   div = document.getElementById('hideShow');
	   switch(el.id){
	   case("sun"):
		   day = 'Sunday';
		   break;
	   case("mon"):
		   day = 'Monday';
		   break;
	   case("tue"):
		   day = 'Tuesday';
		   break;
	   }
	   div.innerHTML = day;
   }
   //-->
  </script>
 </head>
<body>
 <table border="1" cellpadding="3" cellspacing="0" class="regtext" >
  <tr>
   <td id="sun" onclick="toggle(this);">Sun</td>
   <td id="mon" onclick="toggle(this);">Mon</td>
   <td id="tue" onclick="toggle(this);">Tue</td>
  </tr>
 </table>
 <div id="hideShow" class="regtext"></div>
</body>
</html>

Open in new window

0
 
ZvonkoSystems architectCommented:
You are welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.