Mythriel
Mythriel

Reputation: 1380

how to select all divs with the same class

I have several hidden divs on a page , some of them having the same class.

<div class="chapter-1"></div>
<div class="chapter-1"></div>

I prefix my class that way. How do I make all of them to display? I have tried

var id = 1; // get this from other source
$('.chapter-' + id).each().show();

Upvotes: 1

Views: 8111

Answers (4)

Tim
Tim

Reputation: 9489

you don't need the each(). you can just do

$(".classname").show();

so in your case (this will show all elements with class chapter-1).

var id = 1;
$(".chapter-"+id).show();

if you wish to show every div element with a class that starts with 'chapter-' you can use this:

$('div[class|="chapter"]').show();

Upvotes: 13

Tony
Tony

Reputation: 308

To follow up on Tims answer you can also extend it to only select divs if you use the class name for other html entities.

$('div.classname' + id).show();

Upvotes: 0

itsmequinn
itsmequinn

Reputation: 1084

$.('[class ^= "classname"]')

That should do it

Upvotes: 0

Chris Dixon
Chris Dixon

Reputation: 9167

$('.chapter-1').show(); or $('.chapter-1, .chapter-2').show();

Or, if they don't have a unique class, make them all have a unique class. Such as <div class="chapter-1 showDiv"></div> then:

$('.showDiv').show();

Upvotes: 0

Related Questions