DDS
DDS

Reputation: 2479

Select multiple values with jquery

my issue is finding all objects with a specific prefix in the classname, like in the following portion of html I'd like to be able to get us_mr and us_she with a single statement.

<div>
 <div class="us_me">Some about me</div>
 <div class="us_she">Some about she</div>
 <div class="they_she">Some about others</div>
</div>

this what I tried

$(".^us_")

assuming it would have found

<div class="us_me">Some about me</div>
<div class="us_she">Some about she</div>

but got syntax error

Upvotes: 0

Views: 117

Answers (2)

4b0
4b0

Reputation: 22323

Correct syntax for wildcard is: [attribute^="value"].

Example:

$("[class^='us_']").addClass('someClass');
.someClass {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <div class="us_me">Some about me</div>
  <div class="us_she">Some about she</div>
  <div class="they_she">Some about others</div>
</div>

Upvotes: 2

Balastrong
Balastrong

Reputation: 4464

You might want to try with $("div[class^='us_']")

Working demo

console.log($("div[class^='us_']").length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <div class="us_me">Some about me</div>
  <div class="us_she">Some about she</div>
  <div class="they_she">Some about others</div>
</div>

Upvotes: 2

Related Questions