Javascript DOM HTML Element classList Property get class name one by one

Description

Javascript DOM HTML Element classList Property get class name one by one

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Get List of Classes from an Element</title>
<script>
function getClasses(){//from  www  .ja  v a2 s  .  c o m
    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>



PreviousNext

Related