I need to change the text inside
HTML element using javascript, but I have no idea about how to do it. ¿Any help?I've got it defined like:
<h2 id="something">Text I want to change.</h2>
Im trying to do it with:
document.getElementById("something").value = "new text";
But it doesn't work.
Thanks
Use the textContent property to change the text of an element, e.g. element. textContent = 'New text' . The textContent property will set the text of the element to the provided string, replacing any of the existing content. Here is the HTML for the examples in this article.
An H2 tag marks the first sub-heading after your document's main heading. It defines the second-level headings on your webpage. Like an H1 tag, an H2 tag also appears larger than the rest of your main body text.
The HTML DOM allows JavaScript to change the content of HTML elements.
You can use innerHTML:
document.getElementById("something").innerHTML = "new text";
If the element only contains text, textContent
works better and faster than innerHTML
document.getElementById("something").textContent = 'new text';
Good luck :)
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