Getting error "Cannot set property 'innerHTML' of undefined"

Avatar

Avatar

ShagunMalik

Avatar

ShagunMalik

ShagunMalik

08-04-2021

Whenever i'm trying to access data of <p> tag using getElementById in js, then the data is getting accessed but when i try to change the content of <p> tag in js using .innerHTML, I'm getting error UnCaught TypeError:Cannot set property 'innerHTML' of undefined.

How can it be resolved.

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

Anudeep_Garnepudi
MVP

08-04-2021

@ShagunMalik 

HTML

 

<p id="my-id"> </p>

 

Using Vanilla JavaScript

 

document.getElementById("my-id").innerHTML = "Some text";

 

Using jQuery

 

$("#my-id").text("Some text");

 

 

 

 

Answers (0)