Jagd
Jagd

Reputation: 7306

jQuery addClass doesn't work, but css does

Does Not Work

CSS

.active 
{
    display: block;
}

JS

$("#li_1 ul").addClass('active');

Does Work

JS

$("#li_1 ul").css('display', 'block');

Which leads to me my question, why does the css function work and not the addClass? There's no difference between the selectors at all.

Additional info -

Upvotes: 1

Views: 1086

Answers (2)

Abe Miessler
Abe Miessler

Reputation: 85126

Without seeing your HTML it's hard to say for sure why it's not working. I can say that the JS you've posted should work fine. I have a working example here:

http://jsfiddle.net/hsyEw/1/

One thing that jumps out is that this selector $("#li_1 ul") looks a little weird. do you really have a ul element that is a child of an element with an id that implies it is an li?

Your second JS example is applying style directly to the element which will overwrite any CSS Class styles that are being applied, so if you have some other CSS present that is affecting that element this could explain the behavior you are seeing.

Upvotes: 0

ldiqual
ldiqual

Reputation: 15375

What about using this statement:

.active {
  display:block !important;
}

I think something overrides your .active rule in your CSS stylesheet.

More informations about CSS priorities right here.

Upvotes: 4

Related Questions