abdulmanov.ilmir
abdulmanov.ilmir

Reputation: 1375

How to select elements (using jQuery) that have the class attribute with a value beginning exactly with a given string?

I have following elements:

<div class="some-name-1"></div>
<div class="some-name-2"></div>
<div class="some-name-3"></div>
<div class="other-name-1"></div>
<div class="other-name-2"></div>

I need next elements:

<div class="some-name-1"></div>
<div class="some-name-2"></div>
<div class="some-name-3"></div>

I tested jQuery('class^="some-name-"'), but that does not work.
Thanks in advance.

Upvotes: 0

Views: 78

Answers (2)

andyc
andyc

Reputation: 177

You just need to enclose the class^="some-name-" in square brackets: ^ in Selector

$('[class^="some-name-"]')

I'm pretty sure that will work.

Upvotes: 4

JohanSellberg
JohanSellberg

Reputation: 2503

You need [] in that selector.

jQuery('div[class^="some-name-"]')

try that

Upvotes: 3

Related Questions