Eric
Eric

Reputation: 2034

Can I use pseudo classes inside jQuery .css() method?

I have the following jQuery code which works as just the class, but the :hover causes it not to work.

$('.dd_question:hover').css('border','1px dashed #333333');

Is there some special code I can use inside th $() method? Or is :hover simply not allowd?

Upvotes: 2

Views: 194

Answers (2)

user1618143
user1618143

Reputation: 1748

Other people have already explained why the code you're using isn't working, but the other question is how to fix it. Probably the best way to get the effect you're looking for is to have

.hoverborder:hover {border: 1px dashed #333333;}

in your css, and then use

$('.dd_question').addClass('hoverborder');

in your javascript.

Upvotes: 6

Kevin B
Kevin B

Reputation: 95022

The jQuery selector selects elements that match the selector when the code is executed, not when the user hovers over an element.

With that in mind, $('.dd_question:hover') will only select elements that have that class and that were currently being hovered over when the code was executed which most likely was 0 elements.

You need to either use the mouseenter and mouseleave events (or hover for short), or simply use css.

Upvotes: 6

Related Questions