getElementsByClassName().classList.remove() returning undefined Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of getElementsByClassName().classList.remove() returning undefined without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a Marketo form which I want upon form submission to hide all form elements and show a div (class of “thank-you”), and I keep getting the error Uncaught TypeError: Cannot read property 'remove' of undefined. Any ideas what I may be doing wrong?

Fiddle: https://jsfiddle.net/Ld7fajmy/

HTML

<div class="thank-you hide">
  message
</div>

JS

document.getElementsByClassName("thank-you").classList.remove("hide");

Answer

The getElementsByClassName method returns a collection of all elements in the document with the specified class name, as a NodeList object, so you need access it by passing index

document.getElementsByClassName("thank-you")[0].classList.remove("hide");
We are here to answer your question about getElementsByClassName().classList.remove() returning undefined - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji