Instead of using the "id" of the element, you could assign a "class" to the element and then use the mouse events to change the class name.
In this little program, when you mouse over an object it turns gray.
And when you mouse out, the object turns white again unless you click on the object, it will turn another color until you click on it again.
This would be useful if you wish to select only certain objects.
Javascript Code:
<html>
<head>
<style type="text/css">
.white {background-color: white}
.gray {background-color: gray}
.red {background-color: red}
.green {background-color: green}
.blue {background-color: blue}
#div1, #div2, #div3
{
width:100px;
border:1px solid #000;
margin-bottom:5px;
}
#div4 {margin-top:10px;}
</style>
<script language="Javascript" type="text/javascript">
function rollover(obj, v)
{
document.getElementById("div4").style.display = "block";
document.getElementById("rec").innerHTML = "";
switch(v)
{
case 0:
if(obj.className == "gray"){obj.className = "white";}
break;
case 1:
if(obj.className == "white"){obj.className = "gray";}
break;
case 2:
obj.className = obj.className == "red" ? "gray" : "red";
break;
case 3:
obj.className = obj.className == "green" ? "gray" : "green";
break;
case 4:
obj.className = obj.className == "blue" ? "gray" : "blue";
break;
}
}
function display_selected_objects(v)
{
document.getElementById("div4").style.display = "none";
var output = "<p>";
var obj = v.split(",");
for(var i = 0; i < obj.length; i++)
{
if(document.getElementById(obj[i]).className != "white"){output += "<span style='color:blue;'>" + document.getElementById(obj[i]).innerHTML + " is selected.</span><br />";}
else{output += "<span style='color:red;'>" + document.getElementById(obj[i]).innerHTML + " is not selected.</span><br />";}
}
document.getElementById("rec").innerHTML = output;
}
</script>
</head>
<body>
<div id="div1" class="white" onclick="rollover(this,2);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...1</div>
<div id="div2" class="white" onclick="rollover(this,3);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...2</div>
<div id="div3" class="white" onclick="rollover(this,4);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...3</div>
<div id="div4"><a href="#" onclick="display_selected_objects('div1,div2,div3');">Display selected objects.</a></div>
<div id="rec"></div>
</body>
</html>