DOM: Change Element's Content

By Xah Lee. Date: . Last updated: .

This page shows a example of how to change a HTML page's content with JavaScript.

Sample

Press the following buttons to change the paragraph.

How are you?

Code

Here's the HTML code.

<p id="id57512">How are you?</p>
<button id="b1" type="button">Answer</button>
<button id="b2" type="button">Question</button>

Here's the JavaScript code.

function f1() {
 document .getElementById("id57512").innerHTML = "Fine, thank you.";
}

function f2() {
 document .getElementById("id57512").innerHTML = "How are you?";
}

//define the behavior
document .getElementById("b1") .addEventListener( "click", f1 , false);
document .getElementById("b2") .addEventListener( "click", f2 , false);

DOM How-To

  1. DOM: Basic DOM Methods
  2. Get Elements by ID, Tag, Name, Class, CSS Selector
  3. Change CSS
  4. Change Node Content
  5. Create/Insert Node
  6. Remove Node
  7. Get Element's Attribute Value
  8. Set Element's Attribute Value
  9. Remove Element's Attribute Value
  10. List/Add/Remove Class Attribute
  11. Add/Remove Event Handler
  12. DOM: Navigate DOM Tree
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc