Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get the text inside a paragraph with JavaScript

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 !

like image 752
Maroxtn Avatar asked Dec 22 '14 19:12

Maroxtn


People also ask

How do I get text in JavaScript?

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.

How do you use paragraph tags in JavaScript?

The <p> tag defines a paragraph. Browsers automatically add a single blank line before and after each <p> element.

How do I get text inside a div?

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.


1 Answers

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
                                                     ^^^^
like image 81
Oleksandr T. Avatar answered Oct 26 '22 07:10

Oleksandr T.