I’m having a unknown number of elements like so:
<div class="item item-1"></div>
<div class="item item-2"></div>
<div class="item item-3"></div>
What I want to do, is check if each item has a classname starting with “item-“. If true, then extract the id. Something like this:
$("container").each(function
if ($(this).hasClassNameStartingWith("item-"))
console.debug(theId);
);
How is this possible?
Thanks in advance!
Use the contains selector on the class attribute:
Note the use of quotes to include the space, given your sample mark up. You could also omit the space if the “item-N” class could appear at the beginning of the list and you were sure that there weren’t any classes that would accidentally match that string.
Updated example to show how to extract identifier portion of class name.