0

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

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

That's great to hear! You're welcome :)
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.