Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite text in <p> tag using JavaScript

I want to replace the content of a <p> tag only with Vanilla JavaScript. I have something like

<div
    className='container'>
    <p
        className='element' id='replace-me'></p>
</div>

And then I have a function like this

setInterval(function () {
    ...
    document.getElementById('replace-me').innerText(someVar)

}, 1000);

That gives me the error:

TypeError: document.getElementById(...).innerText is not a function

Why is that?

like image 924
Stophface Avatar asked Aug 30 '18 13:08

Stophface


People also ask

How do I change text in P tag?

To change the text font in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <p> tag, with the CSS property font-family, font-size, font-style, etc. HTML5 do not support the <font> tag, so the CSS style is used to change font.

How do you overwrite text in JavaScript?

To replace text in a JavaScript string the replace() function is used. The replace() function takes two arguments, the substring to be replaced and the new string that will take its place.

How do you change the text of an element in JavaScript?

Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content.

Why we use P tag in JavaScript?

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


1 Answers

That's because innerText is an attribute so you should do :

document.getElementById('replace-me').innerText = someVar

instead

like image 92
jonatjano Avatar answered Nov 11 '22 18:11

jonatjano