Paul Done
Paul Done

Reputation: 55

Javascript add class

I have this line in javascript document.getElementById("load").classList.add("active"); but not working. Where is problem and what I must change?

In html I have

<div class="loading" id="load"></div>

And I need with javascript add class active to this div.

Upvotes: 1

Views: 10572

Answers (1)

Hassan AlMandil
Hassan AlMandil

Reputation: 71

Your code looks fine, just make sure your JS code executed after building the DOM, the below code will ensure that:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById("load").classList.add("active");
});

Upvotes: 4

Related Questions