Northernlights
Northernlights

Reputation: 109

Can the :not selector be used with 'this'?

I've got a div called nav, in which some other divs are placed. When I click on one, I want it to change the colour to orange, which is fine - using this.

I'd like the others to remain black when they aren't clicked though.

Can not be used with this?

$('.nav div').click(function() {
    $(this).css('color', 'orange');
    $('.nav div:not(this)').css('color', 'black');
});

Upvotes: 3

Views: 109

Answers (3)

Frank Schwieterman
Frank Schwieterman

Reputation: 24478

I don't think so. Why not set them all to black, then set the special one to orange?

$('.nav').css('color', 'black');
$(this).css('color', 'orange');

Upvotes: 0

ikromm
ikromm

Reputation: 523

You can add a class to all your divs and when you click on one, remove it with jQuery with

.removeClass("<name>");

Be sure to re-add it when you click on another one.

Upvotes: 6

peirix
peirix

Reputation: 37771

You can use the .not() method instead of the selector:

$(".nav div").not(this).css("color", "black");

Or you could use the .siblings() method

$(this).siblings().css("color", "black");

Upvotes: 11

Related Questions