Getting error "Cannot set property 'innerHTML' of undefined" | Community
Skip to main content
Level 3
April 9, 2021
Solved

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

  • April 9, 2021
  • 1 reply
  • 1557 views

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.
Best answer by Anudeep_Garnepudi

@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
Community Advisor
Anudeep_GarnepudiCommunity AdvisorAccepted solution
Community Advisor
April 9, 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");

 

 

 

 

AG