How can I retrieve only the first class p7
from this HTML element?
HTML
<div id="myDIV" class="p7 nextclass class_tt">some content</div>
JavaScript
$('#myDIV').attr('class').first(); //will not work
$('#myDIV').attr('class').split(' ')[0];
$('#myDIV').attr('class')
returns a string. split(' ')
breaks into an array, using ' '
as a delimiter.
You can use split() to do that:
var firstClass = $('#myDIV').attr('class').split(' ')[0];
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