We help IT Professionals succeed at work.

Change font color onmouseover

yourbudweiser
on
10,690 Views
Last Modified: 2009-02-20
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?
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Top Expert 2007

Commented:
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>
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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>



Top Expert 2007

Commented:
Sorry, hadn't seen basicinstinct's post, should have refreshed...
lol @ cornflowerblue
ZvonkoSystems architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
:)
yours is quite different to mine tho tname, certainly worth posting alongside mine as it gives yourbudweiser more options to consider...

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.