How to set the last-clicked anchor to be a different color from all other links?
11,658
Solution 1
It wouldn't require jQuery, but it's sure easy to do with jQuery.
$("a").click(function () {
$("a").css("color", "blue");
$(this).css("color", "yellow");
});
Solution 2
You don't need Javascript. The CSS pseudo-class that you're looking for is 'focus'.
ps: it holds the 'last clicked' color only until you click on something else in the page.
a:link {color:#00FF00}
a:visited {color:#00FF00}
a:hover {color:#FF00FF}
a:focus {color:#0000FF} /* this one! */
<b>
<a href="#">link 1</a>
<a href="javascript:void(0);">link 2</a>
<a href="#">link 3</a>
<a href="javascript:void(0);">link 4</a>
</b>
Solution 3
You definitely can't do it with css.
With jQuery you could do something like
$("a").live("click", function() {
$("a").removeClass("yourHighlightClass");
$(this).addClass("yourHighlightClass");
});
Author by
blueberry pancake
Updated on June 05, 2022Comments
-
blueberry pancake almost 2 years
a:link {color:#FF0000} /* unvisited link */ a:visited {color:#00FF00} /* visited link */ a:hover {color:#FF00FF} /* mouse over link */ a:active {color:#0000FF} /* selected link */
The pseudo-classes (link, visited, hover, active) don't do exactly what I want which is to highlight the last-clicked link on a page to be a different color from all of the other links on the page.
Would this require JQuery and, if so, any suggestions?
-
blueberry pancake almost 15 yearsWhy do you use a.bind instead of a.click? I'm not familiar with bind.
-
blueberry pancake almost 15 yearsWow. That's nice. Can't believe how little code it takes. Thanks.
-
eyelidlessness almost 15 yearsblueberry pancake, click(callback) is a shortcut to bind('click', callback)
-
Adam A almost 15 yearsYou don't use classes, plus you duped my answer 4 minutes later. I didn't think it added anything. No offense meant.
-
nuiun almost 15 yearsIt is certainly not a dupe, and I resent the accusation. I hope you don't downvote answers in all the threads that you yourself personally answer.
-
Valkea about 7 yearsThis should be the accepted answer. It works and it is so simple :)