Reputation: 201
New to JS and jQuery, designing a game as an exercise.
I'm trying to select divs whose IDs contain a certain string AND have a certain class.
I know how to select a class:
$(".myClass")
and I know how to select for IDs that include a certain string:
$("[id*=" + certainString + "]")
But I do not know how to combine the two to get this:
$( elements of class ".myClass" that also have id that contains certainString)
Upvotes: 0
Views: 35
Reputation: 370729
Just put the selectors right next to each other (without a space in between) to select elements which have a certain class and also have an id
matching the constraints:
const certainString = 'foo';
const fooMyClasses = $(".myClass[id*=" + certainString + "]");
console.log(fooMyClasses.length);
console.log(fooMyClasses[0].outerHTML);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="myClass">
</div>
<div class="myClass" id="foo123">
</div>
Upvotes: 1