I am trying to get the text inside p tag
<p id="paragraph">Some text</p>
And i want to store the text in JavaScript variable like this
var text = "Some text";
I read that i have to add
var text = document.getElementById("paragraph").innerHtml;
But the value of the variable is NULL or Undefined and i did also like this
var text = document.getElementById("paragraph")[0].innerHtml;
And i had the same problem !
Use the textContent property to get the text of an html element, e.g. const text = box. textContent . The textContent property returns the text content of the element and its descendants. If the element is empty, an empty string is returned.
The <p> tag defines a paragraph. Browsers automatically add a single blank line before and after each <p> element.
Use the textContent property to get the text of a div element, e.g. const result = element. textContent . The textContent property will return the text content of the div and its descendants. If the element is empty, an empty string is returned.
In your example you are using incorrect property name, must be innerHTML
(note - case sensitivity HTML
not Html
) not innerHtml
var text = document.getElementById("paragraph").innerHTML
^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With