Dan
Dan

Reputation: 16226

JQuery - addClass when the class is bound to an id

I have this CSS rule:

#panelSeparator .active {
    background: #fff url(../img/vgrabber.gif) no-repeat center;
    opacity: 0.7;  
}

I would like to do something like this in JS:

$('#panelSeparator').addClass('active');

How should I change the parameter of addClass in order to make that work?

Thanks,
Dan

Upvotes: 2

Views: 143

Answers (4)

zod
zod

Reputation: 12417

Did you try,

$('#panelSeparator').addClass('panelSeparator active');

Upvotes: 0

thedaian
thedaian

Reputation: 1319

If you want to keep the same CSS, then you'll need to do something like $('#panelSeparator div').addClass('active'); in Javascript. Basically, you'd have to select an element inside the #panelSeparator element.

Or you can keep the same Javascript and just change the CSS to #panelSeparator.active

Upvotes: 0

Brian Fisher
Brian Fisher

Reputation: 23989

Try removing the space between #panelSeparator .active so it is #panelSeparator.active.

Upvotes: 8

giorgio
giorgio

Reputation: 10202

your css defines a class which is bound to all children of elements with the id 'panelSeperator'. You should use the jQuery.children() function (http://api.jquery.com/children/);

$('#panelSeparator').children().addClass('active');

Upvotes: 0

Related Questions