JS - if elements has class

0 votes
137 views
added Jan 24, 2019 in Javascript by LC Marshal Captain (25,770 points)
<html>
   <body>
      <div id="myelement" class="classElement"></div>
      <script>
         function hasClass(element, clas) {
            return (' ' + element.className + ' ').indexOf(' ' + clas + ' ') > -1;
         }
         var val1 = document.getElementById('myelement');
         alert(hasClass(val1, 'classElement'));
     </script>
   </body>
</html>

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...