Might be a newbie question. I have a code line like this:
<div class="template active">
I need to get each class for itself.
I tried this code:
$(this).attr("class");
From that code I get "template active". What I need is one string with "template" and another with "active".
What is the best jQuery function for that? Example?
var classes = $(this).attr("class").split(/\s/);
classes[0] === 'template'
classes[1] === 'active'
If there are more than two classnames and you only want to get the first & last (that was your question) you can call:
classes[0] = first class
classes[classes.length -1] = last class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With