So I have some nav anchors. I want to change some basic color schemes on the page when these are clicked. The only way I’ve been able to do this is with the code below.
It works fine, but I can’t help but think there is a more elegant way. Does anybody know if it’s possible to strip the classes in one foul swoop?
EDIT: This could have been more clear. I didn’t want the original classes gone. Just the classes added with jQuery I guess. Sorry for the confusion.
Thanks.
EDIT: SEE THIS FIDDLE
Tried to implement all solutions.
getting an “{“error”: “Please use POST request”}” though…
EDIT: this was because of href=”?” vs. href=”#” as pointed out.
$(".home").click(function() {
$(".primary-color").addClass("one");
$(".primary-color").removeClass("two");
$(".primary-color").removeClass("three");
$(".primary-color").removeClass("four");
$(".primary-color").removeClass("five");
});
$(".services").click(function() {
$(".primary-color").addClass("two");
$(".primary-color").removeClass("one");
$(".primary-color").removeClass("three");
$(".primary-color").removeClass("four");
$(".primary-color").removeClass("five");
});
$(".pricing").click(function() {
$(".primary-color").addClass("three");
$(".primary-color").removeClass("one");
$(".primary-color").removeClass("two");
$(".primary-color").removeClass("four");
$(".primary-color").removeClass("five");
});
$(".special-thing").click(function() {
$(".primary-color").addClass("four");
$(".primary-color").removeClass("one");
$(".primary-color").removeClass("two");
$(".primary-color").removeClass("three");
$(".primary-color").removeClass("five");
});
$(".contact").click(function() {
$(".primary-color").addClass("five");
$(".primary-color").removeClass("one");
$(".primary-color").removeClass("two");
$(".primary-color").removeClass("three");
$(".primary-color").removeClass("four");
});
You could do it this way
http://jsfiddle.net/lollero/ZUJUB/
CSS:
HTML:
or something like this:
http://jsfiddle.net/lollero/ZUJUB/1/
This fitted into the OP’s structure: http://jsfiddle.net/lollero/EXq83/5/
jQuery:
CSS:
HTML: