English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
HTML DOM allows JavaScript to get and change the content of HTML elements.
The simplest way to change the content of an HTML element is to useinnerHTMLproperty.
To change the content of an HTML element, please use the following syntax:
element.innerHTML = text
The following examples useid="para" change<p>The content of the element:
<!DOCTYPE html> <html> <p id="para"></p> <script> document.getElementById("para").innerHTML = "Hello world"; </script> </html>Test and See‹/›
The following exampleanchor elementid="para"of<p>The content of the element:
<!DOCTYPE html> <html> <p id="para">This is a paragraph.</p> <p id="result"></p> <script> var x = document.getElementById("para").innerHTML; document.getElementById("result").innerHTML = x; </script> </html>Test and See‹/›
document.write()method writes a text string to the document stream.
<!DOCTYPE html> <html> <p>The document.write() method writes a text string to the document output stream:</p> <script> document.write(new Date()); </script> </html>Test and See‹/›
This method writes content to the current document only when the document is parsed.
If this method is used after the page is loaded, it will overwrite all existing content in the document.
<button onclick="myFunc()">Click me</button> <script> function myFunc() { document.write(new Date()); } </script>Test and See‹/›
To change the value of an HTML attribute, use the following syntax:
element.attribute = new value
The following examplechange<img>elementsrcattribute value:
The following exampleanchor elementto get thehrefattribute value:
var x = document.getElementById("demo").href;Test and See‹/›
You can usedocument.createElement()method explicitly creates a new element in the HTML document.
This method creates a new element but does not add it to the DOM. You must perform this operation in a separate step:
// Create a new h3element var newElem = document.createElement("h3"); // and give it some content var newContent = document.createTextNode("Hello, everyone!");
appendChild()The method adds the new element to any other child node of the specified parent nodeend.
// Add text nodes to the newly created h3 newElem.appendChild(newContent); // Add the newly created element and its content to the DOM document.body.appendChild(newElem);Test and See‹/›
However, if you want to add it to any other sub-itembeginningTo add a new element, you can use theinsertBefore()method.
//Create a new h3element var newElem = document.createElement("h3"); // Give it some content var newContent = document.createTextNode("Hi there and greetings!"); // Add text nodes to the newly created h3 newElem.appendChild(newContent); // Get the Body var body = document.body; // Insert H3 before the first child of the BODY body.insertBefore(newElem, body.childNodes[0]);Test and See‹/›
Similarly, you can use theremoveChild()Method to remove a child node from the DOM.
var div = document.getElementById("demo"); div.removeChild(div.firstElementChild);Test and See‹/›