Accessing null element | Sololearn: Learn to code for FREE!
+ 2

Accessing null element

https://sololearn.com/compiler-playground/WH5arb1ky65v/?ref=app I am trying to add an event listener to an <input/>. Every time I run the program, I get an error stating that I am trying to access the property of null. These are some checks I have done: 1. The id name is correct 2. The <input/> had an id, which is the same as the id name in the getElementById method Is this a general problem with the sololearn playground? Or what am I doing wrong?

13th Jun 2024, 3:31 PM
Samuella M Aglago
3 Answers
+ 5
To ensure all elements in DOM are loaded successfully , insert your entire js code in:- window.onload = () => { //your js code } There is another way too just use script tag in html editor and place your js code inside it.
13th Jun 2024, 3:36 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 4
in order to access the html element with js, you need to make sure that it is already loaded load event). one way to deal with it is to put your js code in the function that is only executed once the dom is loaded. window.onload = () => { // all your js here }
13th Jun 2024, 3:36 PM
Lisa
Lisa - avatar
+ 4
Thank you all, it works now Gulshan Mahawar Lisa
13th Jun 2024, 3:49 PM
Samuella M Aglago