Code Snippet
Exclude $(this) from Selector
Let's say you want to attach a click handler to every link on a page. The function for that click handler turns all the other links a different color.
var $allLinks = $("a");
$allLinks.click(function() {
$allLinks.not(this).css("color", "red");
});You can use the .not() function to remove elements from a set, so padding this to that function will remove the current element before the color change.
Better application for this:
Since having a lot of links on a page with this script means that jQuery will manipulate each of the elements and do stuff in the class-attributte, this can become quite expensive and slow.
A better way would be to use a common wrapper class.
That way jQuery only touches the current link and the previously clicked ones. Way faster and cheaper on larger pages and good practice in general.
The body-class part is not mandatory, the links could be made red by default leaving only two css rules. and two lines of javascript within the click function.
See also http://krinkle-tools.grizzdesign.nl/other-anchors.html
A part went missing in the comment, see the link:
http://krinkle-tools.grizzdesign.nl/other-anchors.html
For the correct javascript and css
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.