Getting error "Cannot set property 'innerHTML' of undefined" | Community
Skip to main content
Level 3
Apr 9, 2021 3:45 AM UTC
Solved

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

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.

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Anudeep_Garnepudi
Anudeep_GarnepudiAccepted solution
New Participant
Apr 9, 2021 4:07 AM UTC

@shagunmalik 

HTML

 

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

 

Using Vanilla JavaScript

 

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

 

Using jQuery

 

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

 

 

 

 

1 reply

Anudeep_Garnepudi
Anudeep_GarnepudiAccepted solution
New Participant
Apr 9, 2021 4:07 AM UTC

@shagunmalik 

HTML

 

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

 

Using Vanilla JavaScript

 

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

 

Using jQuery

 

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

 

 

 

 

Translate