Victor
Victor

Reputation: 23922

Selector for multiple tags inside a div with id

In jQuery, how can I build a selector for tags h1, h2, h3 and p inside #con?

Like $('#con h1, #con h2, #con h3, #con p') but without repeating the #con

Upvotes: 9

Views: 13158

Answers (2)

Code Maverick
Code Maverick

Reputation: 20415

You can do any of the following:

$("#con h1, #con h2, #con h3, #con p")   // your original

$("h1, h2, h3, p", $("#con"))            // pass jQuery object in as context

$("h1, h2, h3, p", "#con")               // pass selector in as context

$("#con").find("h1, h2, h3, p")          // do what jQuery ultimately does 
                                         // in the end when passing context
                                         // as jQuery or as string selector

I've created a nice little jsFiddle that demos all of this.

Upvotes: 21

Jessie A. Morris
Jessie A. Morris

Reputation: 2317

$('h1, h2, h3, ...', $('#con')) would work. This sets the context to be #con and searches for h1, h2, etc. inside of the context (#con).

Upvotes: 5

Related Questions