user790843
user790843

Reputation: 133

Question about select all H3 inside DIV

Wouldn't this work if I want to apply a new class to all H3 tags inside the RelArtik div?

$("h3",$("#RelArtik")).addClass("underrubrik");

Thanks.

Upvotes: 1

Views: 5711

Answers (3)

Alnitak
Alnitak

Reputation: 339957

According to the documentation jQuery should accept a jQuery object as the context so there's no obvious reason why what you've written shouldn't work.

However, it also says that:

$(selector, context)

is equivalent to:

$(context).find(selector)

So you could just try:

$('#RelArtik').find('h3').addClass(...);

which is of course also equivalent to:

$('#RelArtik h3').addClass(...);

however I believe the former .find() based solution is faster.

Upvotes: 4

ximi
ximi

Reputation: 596

Alternatively you can just do:

$('#RelArtik h3').addClass('underrubrik');

Upvotes: 0

clairesuzy
clairesuzy

Reputation: 27654

it's the same as CSS and would work with a descendant selector

$("#RelArtik h3").addClass("underrubrik");

Upvotes: 3

Related Questions