Rasel Ahmed
Rasel Ahmed

Reputation: 305

jQuery selectors for all div

I have my div like this

<div id="gallery-1"></div>
<div id="gallery-2"></div>
<div id="gallery-3"></div>
<div id="gallery-4"></div>
<div id="gallery-5"></div>

How can I select them in one jquery selector? like

jQuery("#gallery-*").something();

Any Ideas?

Upvotes: 0

Views: 66

Answers (3)

Luka
Luka

Reputation: 1718

jQuery("div") or change id to class inside your HTML and specify the following jQuery code: jQuery(".gallery").

You can also use more advanced selector syntax like jQuery("div[id='^gallery']"), as Rajaprabhu suggested.

Upvotes: 0

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You can use start with selector

jQuery('div[id^=gallery]').something();

Upvotes: 0

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use attribute starts with selector at this context,

jQuery("[id^='gallery-']").something();

Or the best way would be using a common class,

HTML:

<div id="gallery-1" class='test'></div>
<div id="gallery-2" class='test'></div>
<div id="gallery-3" class='test'></div>
<div id="gallery-4" class='test'></div>
<div id="gallery-5" class='test'></div>

JS:

 jQuery(".test").something();

Upvotes: 6

Related Questions