Change link color

If I have 5 links. When first, if I click on a link, it should change to a different color, and then if I click on a link to another link, the color of the previous link should go to the default value, and that color of the link should change

THE CODE:

$("table#menu tr > td a[href^='#']").click(function() { 
     $(this).toggleClass('class1'); / 
}); 

a.class1 { color:#000000; } 

<table id="menu"> 
<tr> 
   <td><a href="#" id="link1">qwerty</a></td> 
</tr> <tr> 
   <td><a href="#" id="link1">zyz</a></td> 
</tr> 
</table>
+5
source share
4 answers

This should answer your question:

http://jsfiddle.net/TL9rh/

HTML

<div id="links">
    <a href="#">link1</a>
    <a href="#">link2</a>
    <a href="#">link3</a>
    <a href="#">link4</a>
    <a href="#">link5</a>
</div>

Javascript

$(document).ready(function(){
    $('#links a').click(function(){
        $(this).addClass('selected');
        $(this).siblings().removeClass('selected');
    });
})

CSS

a {
    color: darkgreen;   
}


.selected {
     color: red;   
}
+2
source

Classes.

CSS

a {
    color: green;
}
a.special {
    color: orange;
}

JavaScript:

$('a').click(function(evt) {
    evt.preventDefault(); //don't follow link
    //remove the special class from all links which already have it
    $('a.special').removeClass('special');
    //add the special classs to the clicked link
    $(this).addClass('special');
}

Of course you have to change the selector according to your html.

Real-time example: http://jsfiddle.net/KHjDr/

+6
source

, , , CSS.

HTML

<a href="#">MyLink1</a>
<a href="#">MyLink2</a>
<a href="#">MyLink3</a>

CSS

a:link{color: blue;}
a:active{color: red;}
0

jQuery:

$(document).ready(function(){
$('.win a').click(function(){
    $('.win a:first-child').removeClass('focused');
    $(this).addClass('focused');

});

})

The Html:

<li class="widget-title win"><a class="focused" href="#window1">Recent News</a></li>
<li class="widget-title win"><a href="#window2">Most Favorites</a></li>
<li class="widget-title win"><a  href="#window3">Top News</a></li>
0

All Articles