Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Get List of Classes from an Element</title> <script> function getClasses(){ var classList = document.getElementById("myDiv").classList; for(var i = 0; i < classList.length; i++){ document.body.insertAdjacentHTML("beforeend", "<p>" + classList[i] + "</p>"); } } </script> </head> <body> <div id="myDiv" class="foo bar bazz"> <p>This is a paragraph inside DIV element</p> </div> <button type="button" onclick="getClasses();">Get Div Class List</button> </body> </html>