0

window.onload = function what() {
  var a = document.getElementById("abc");
  a.innerHTML = 'hi';
};
<div id="editor">
  <p id="abc">Class </p>{ public void add(int a, int b){ int sum=a+b; } }
</div>
empiric
  • 7,449
  • 6
  • 35
  • 44
  • please refer to this code below... typo mistake in the above code...

    Class

    { public void add(int a, int b){ int sum=a+b; } }
    – arpit gupta Dec 16 '19 at 14:49
  • 2
    Don't post code in comments, you can edit your question – empiric Dec 16 '19 at 14:50
  • 1
    @arpitgupta Please edit the question with correct code. – stud3nt Dec 16 '19 at 14:50
  • Well the code you posted works, so something is missing here – empiric Dec 16 '19 at 14:51
  • That code has no issues so it is related to something else that you are not showing. – epascarello Dec 16 '19 at 14:54
  • Does this answer your question? [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – VLAZ Dec 16 '19 at 15:05
  • @epascarello correct. My assumption is that the `"abc"` element is not created when `onload` happens. – VLAZ Dec 16 '19 at 15:06

1 Answers1

0

document.getElementById() either returns a DOM node or null, based on whether an element with the given id exists in the HTML document.

Since your error message says "Cannot set property 'innerHTML' of null", it means that no element with id "abc" was found.

Pranjal Nadhani
  • 106
  • 1
  • 5