Get the first class from an element using jQuery

How can I get only the first p7 class from this HTML element?

HTML

 <div id="myDIV" class="p7 nextclass class_tt">some content</div> 

Javascript

 $('#myDIV').attr('class').first(); //will not work 
+4
source share
2 answers
 $('#myDIV').attr('class').split(' ')[0]; 

$('#myDIV').attr('class') returns a string. split(' ') split into an array using ' ' as a separator.

+10
source

You can use split () to do this:

 var firstClass = $('#myDIV').attr('class').split(' ')[0]; 
+4
source

Source: https://habr.com/ru/post/1346931/


All Articles