ned
ned

Reputation: 71

Simple classlist.add("someclass") always giving an error

This is a simple representation of what I'm facing

var box=document.getElementsByClassName('foo')
    box.classList.add('things')
    .foo{
        height: 50px;
        width: 50px;
        background-color: aqua;
        border: 1px solid royalblue;
    }
    .things{
        border-radius: 50%;
    }
<body>
    <div class="foo"></div>
</body>

Javascript console always returns these. Uncaught TypeError: Cannot read property 'add' of undefined Is there anything I'm missing?

Upvotes: 0

Views: 41

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28434

getElementsByClassName returns a list, so you need to specify the index as follows:

var box=document.getElementsByClassName('foo')[0];

Upvotes: 2

Related Questions