get only the first class of an element - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:attr

Description

get only the first class of an element

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js"></script> 
  <script type="text/javascript">
    window.onload=function(){/*from ww  w  . j a  v  a  2s . co  m*/
$("div").click(function(){
  var classString = $(this).attr('class');
  var classArray = classString.split(' ');
  console.log(classArray);
});
    }

      </script> 
 </head> 
 <body> 
  <div class="blog blag bog">
    The div 
  </div>  
 </body>
</html>

Related Tutorials