0

I'm trying to manipulate the text-color of a certain div using javascript. When trying to run the following code:

document.getElementById("63b71485db82c3005065f1eabf961985").style.color = "#42f45f";

I get the error "Cannot read property 'style' of null" UNLESS I right-click and inspect the div and then execute the code.

  • 4
    Please share a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ele Feb 23 '18 at 14:29
  • #1 cause of this sort of problem is the code being placed in the document such that it runs before the target element has been parsed and added to the DOM. – Pointy Feb 23 '18 at 14:29
  • 1
    i bet the element with id `63b71485db82c3005065f1eabf961985` is generated dynamically? It is not present to the dom when you execute the code but it got appended to the DOM afterwards, so the code runs in the console – messerbill Feb 23 '18 at 14:30
  • @epascarello: I've done the right one now. :-) (BTW, you can fix that as of a fairly recent SO update. You can use the edit link on the list of dupetargets, add in the one you meant, and remove the one you did by mistake.) – T.J. Crowder Feb 23 '18 at 14:31
  • @T.J.Crowder I need more coffee :) – epascarello Feb 23 '18 at 14:31
  • The id is not dynamic in the way that it doesn't change from time to time. While testing I've just executed from the console. But what you are saying @messerbill seems reasonable since I don't know if the id is present until I inspect the element. – Frederik Bruun Feb 23 '18 at 14:42

0 Answers0