What is wrong with this js code?

Hello Campers

please check my code and let me know if something wrong.
why color not changing ?

and many thanks in advance.

It might help if you state:

  • what you’re trying to do with this code,
  • what’s not working, and
  • what you’ve tried so far.
1 Like

Create a habit of looking at the browser console (developer tools) when something doesn’t work as expected.

In your case, you have this error (to begin with) -

Uncaught ReferenceError: getElementById is not defined
    at pen.js:4
1 Like

thanks for replying
what I am trying to do is when I scroll the page, the color of words inside the div changes from yellow to red when the div appears

but with this code, the color of words not changing

thanks so much for replying

yes I just have noticed this on the console now, so what should I do?

var div = getElementById("change");

This line implies that your code has a getElementById function that you defined somewhere in your code. When that function is not found, you get the error -

Uncaught ReferenceError: getElementById is not defined
    at pen.js:4

Unless you meant to define your own function by that name, you probably meant to use document.getElementById(). You need to use the right syntax to be able to use that function. Read about it on MDN.

Next, in this line - div.style.color = red ; - red without quotes refers to a variable red. You should use the string "red" instead.

Lastly, you’ll need to look at the scrolling logic.

I would recommend going through the freeCodeCamp curriculum in order first.