Link to home
Start Free TrialLog in
Avatar of yourbudweiser
yourbudweiser

asked on

Change font color onmouseover

I have a bunch of asp.net labels. I change the text color to blue with onmouseover="this.style.color='blue';" and I change it back to black with onmouseout="this.style.color='black';"

I would like to keep this functionality but also when the label text is clicked, I want it to remain blue. The mouseover effects will still funtion but if I click a different label, the current one will go to black and the new one will be blue.

I already have a function attached to the onclick event as well.

Can this be done?
ASKER CERTIFIED SOLUTION
Avatar of basicinstinct
basicinstinct
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TName
TName

This is somewhat different, as it uses LIs, css hover instead of onmouseover, and the js triggers the stay-clicked effect by changing the css class of the elements, but the basic functionality is pretty much the one described by you (if I understand it correctly):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script>
function changeCol(obj){    
  var links = document.getElementsByTagName('A');
   for (var i=0; i<links.length; i++) {
     if (links[i].className=='highlighted' && links[i] != obj)  
           links[i].className='plain';  
  }
  obj.className='highlighted';
}
</script>
<style>
#content {width:300px; overflow:hidden; margin:0 auto;}
ul#tabs {float:left; width:240px; list-style: none;  margin:0; padding:0; height:20px;}
ul#tabs li {list-style: none; display:inline; float:left; margin:0 4px; }

ul#tabs li a,
a.plain {display:block; background:#fff; color:#000; }

ul#tabs li a:hover,
ul#tabs li a.highlighted  {background:#abc; color:#00f;}
</style>
</head>
<body>
<div id="content">
    <ul id="tabs">
       <li><a class="plain" href="#" onclick="changeCol(this);">Tab one</a></li>
       <li><a class="plain" href="#" onclick="changeCol(this); ">Tab two</a></li>
       <li><a class="plain" href="#" onclick="changeCol(this);">Tab three</a></li>
    </ul>
</div>
</body>
</html>
Avatar of Zvonko
How about this:


<html>
<head>
<title>Zvonko &#42;</title>
<style>
li.blue {
  background-color: cornflowerblue;
}
</style>
<script>
var lAct;
function doHover(theLink){
  theLink.className="blue";
}
function doLeave(theLink){
  if(lAct!=theLink){
    theLink.className="";
  }
}
function setLink(theLink){
  if(lAct) lAct.className="";
  lAct=theLink;
  lAct.className="blue";
}
</script>
</head>
<body>
<ul>
<li onmouseover="doHover(this)" onmouseout="doLeave(this)" onClick="setLink(this)" >One</li>
<li onmouseover="doHover(this)" onmouseout="doLeave(this)" onClick="setLink(this)" >Two</li>
<li onmouseover="doHover(this)" onmouseout="doLeave(this)" onClick="setLink(this)" >Three</li>
<li onmouseover="doHover(this)" onmouseout="doLeave(this)" onClick="setLink(this)" >Four</li>
<li onmouseover="doHover(this)" onmouseout="doLeave(this)" onClick="setLink(this)" >Five</li>
</ul>
</body>
</html>



Sorry, hadn't seen basicinstinct's post, should have refreshed...
lol @ cornflowerblue
:)
yours is quite different to mine tho tname, certainly worth posting alongside mine as it gives yourbudweiser more options to consider...
Avatar of yourbudweiser

ASKER

these look great, thanks so much for responding. i'll need some time to check out the code.