jquery - How to select part of a specific css class name out of multiple classes -
given following html markup:
<div id="my-id" class="my-class items-5"/>
how can extract 5
class attribute, if 5
generated dynamically? split on ' '
, reference index of '-'
on second index, seems long winded:
var count = $("#my-div").attr("class").split(' ')[1].split('-')[1];
another solution fixed markup can't change, , assuming relevant class items-number
split items-
, , first word after it:
var count = $("#my-id").attr("class").split('items-')[1].split(' ')[0]; console.log(count); var count2 = $("#my-id2").attr("class").split('items-')[1].split(' ')[0]; console.log(count2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="my-id" class="my-class items-5"/> <div id="my-id2" class="my-class items-23 another-class"/>
this work more classes , different orderings.
Comments
Post a Comment