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?
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.
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.
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.
Definition and Usage. The <p> tag defines a paragraph. Browsers automatically add a single blank line before and after each <p> element.
That's because innerText
is an attribute so you should do :
document.getElementById('replace-me').innerText = someVar
instead
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