0

I'm learning DOM manipulation. I have a problem about DOM. I used this code to h1's color in console. It works!.

var h1 = document.querySelector("h1");
h1.style.color = "pink";

But when I'm using Sublime text coding same code, chrome console giving me errors. Hoping someone help me.Thanks.

Error Message

Robbie
  • 17,090
  • 4
  • 31
  • 66
n1sAm1
  • 9
  • 1
  • You loaded your JavaScript file in the head of your document. The DOM hadn't rendered yet. There was no element type `h1` available to select when queried. The result was null from the querySelector. You attempted to access the style property of null and encountered an error. Load your JavaScript file *after* the DOM has rendered by placing the script tag lower in the page, or use window.onload to wait for the DOM to render. – Travis J Aug 25 '18 at 04:53
  • please show what is your error? – vibol Aug 25 '18 at 04:54
  • Travis is right. This error occurs in scenario when DOM is not rendered/loaded. Hence h1 tag wouldn't be present in DOM tree. – Meet Zaveri Aug 25 '18 at 04:58
  • Thanks Travis J and vibol. I got it.I have a note about that too. When you told me, I remembered.Thank you very much.Coding is exciting even more now. – n1sAm1 Aug 25 '18 at 04:58

0 Answers0