Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Having trouble toggling the class of elements in an Array

Im trying to toggle the "done" class of the elements in an array on click but I keep getting error messages saying "Uncaught ReferenceError: classList is not defined". When I try to console.log("testing") on clicking it works so I know I'm close I just cant figure it out.

var button = document.getElementById("enter");
var input = document.getElementById("userinput");
var ul = document.querySelector("ul");
var li = document.querySelectorAll("li");

function inputLength() {
    return input.value.length;
}

function createListElement() {
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(input.value));
    ul.appendChild(li);
    input.value = "";
}

function addListAfterClick() {
    if (inputLength() > 0) {
        createListElement();
    }
}

function addListAfterKeypress(event) {
    if (inputLength() > 0 && event.keyCode === 13) {
        createListElement();
    }
}

function toggleDone() {
    classList.toggle("done");
}

for (var i = 0; i < li.length; i++) {
    li[i].addEventListener("click", toggleDone);
}

button.addEventListener("click", addListAfterClick);

input.addEventListener("keypress", addListAfterKeypress);




<html>
<head>
    <title>Javascript + DOM</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <h1>Shopping List</h1>
    <p id="first">Get it done today</p>
    <input id="userinput" type="text" placeholder="enter items">
    <button id="enter">Enter</button>
    <ul>
        <li>Notebook</li><button id="delete">Delete</button>
        <li>Jello</li><button id="delete">Delete</button>
        <li>Spinach</li><button id="delete">Delete</button>
        <li>Rice</li><button id="delete">Delete</button>
        <li>Birthday Cake</li><button id="delete">Delete</button>
        <li>Candles</li><button id="delete">Delete</button>
    </ul>
    <script type="text/javascript" src="script.js"></script>

</body></html>
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!