How to get the DIV class by ID

To get the div classes by its ID, use className.

document.getElementById("myElement").className

Example:

<div id="myElement" class="myClass">

<script>
    var class = document.getElementById("myElement").className;
    console.log(class);
</script>

Output (console): myClass

How to get multiple DIV classes by ID

It’s the exact same way as with one class.

Example:

<div id="myElement" class="myClass1 myClass2">

<script>
    var classes = document.getElementById("myElement").className;
    console.log(classes);
</script>

Output (console): myClass1 myClass2

If you want to access class names separately, push them into an array with split() method.

Example:

<div id="myElement" class="myClass1 myClass2">

<script>
    var classes = document.getElementById("myElement").className;
    
    classes = classes.split(' ');

    console.log(classes);
    console.log(classes[0]);
</script>

Output (console):

(2) [‘myClass1’, ‘myClass2’]

myClass1