I have the following structure:
<li class="header group1"></li>
<li class="group1"></li>
<li class="group1"></li>
<li class="group1bottom"></li>
<li class="header group2"></li>
<li class="group2"></li>
<li class="group2"></li>
<li class="group2bottom"></li>
.. and so on
I get the headers:
let headers = $('.header');
then I need to loop through each of the headers elements, extract the other class containing the groupx
, and match each of it with it’s corresponding groupxbottom
:
headers.each(function() {
// hide each groupxbottom element
});
I’ve tried a few attempts using $(this).classList[0]
with regex to extract the specific class but could not get the correct result
2
Answers
You can do it like this:
var n = $(this).attr("class").match(/group(d+)/)[1];
will extract the number from the corrospondinggroupX
classDemo