Jason
Jason

Reputation: 4142

JQuery Wildcard ID Selector With Class

I need to create a jquery selector to select all elements with an ID starting with newInstruction, with a class of .individualInstruction.

This should match divs with id newInstruction0, newInstruction1, etc.

I can do this separately like this:

$('[id^="newInstruction"]')

and

$('.individualInstruction')

However, I'm getting syntax errors when I try to combine them. This is probably simple, but is there a jquery guru out there than can point out what I did wrong?

Upvotes: 0

Views: 392

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30567

Combine them with no space like

$('[id^="newInstruction"].individualInstruction')

document.write($('[id^="newInstruction"].individualInstruction').length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div id="newInstruction1" class="individualInstruction"></div>
<div id="newInstruction2" class="individualInstruction"></div>
<div id="newInstruction3" class="individualInstruction"></div>

Upvotes: 4

Related Questions