0

I managed to make the code work using a for loop. However, I thought it could be more efficient to use the forEach method instead. How do I tweak my code so that it does what's expected to be doing? I'm might sound very basic, but I'm still a beginner.

const list = document.getElementsByTagName("li");

list.forEach(myFunction())

function myFunction(element, index) {
  element[index].className = "newColor"
}
.newColor {
  color: green;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Practise App</title>
    <link rel="stylesheet" href="index.css">
  </head>
  <body>

  <ul class="green" src="josue">
    <li>Monday</li>
    <li>Tuesday</li>
    <li>Wednesday</li>
    <li>Thursday</li>
    <li>Friday</li>
    <li>Saturday</li>
    <li>Sunday</li>
  </ul>
    <script src="index.js" type="text/javascript"></script>
  </body>
</html>
  • 1
    `list.forEach(myFunction())` -> `list.forEach(myFunction)` – VLAZ Feb 02 '21 at 09:57
  • 1
    `for (const [key, value] of Object.entries(list)) { myFunction(value); } function myFunction(element) { return element.className = "newColor" } ` – antoineso Feb 02 '21 at 10:07

0 Answers0