In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML.
- Using getElementById() method: This method gets/identifies the DOM elements using it’s ID and returns the element.
Example:
<!DOCTYPE html><html><head><title>How to get/change the HTMLwithDOM elementinJavaScript?</title></head><body><h3>Accessing HTML of a DOMelementinJavaScript.</h3><p id="iD1">JavaScript is usedforweb programming.</p><button onclick=getHtml()>Get and change the htmlforDOM element</button><script>functiongetHtml() {varElement = document.getElementById("iD1");alert(Element.innerHTML);Element.style.color ="orange";Element.innerHTML ="GeeksforGeeks";}</script></body></html>chevron_rightfilter_noneOutput:
- Before Clickin the Button:

- Aftr Clicking the Button:

- Before Clickin the Button:
- Using getElementByName() method: This method gets/identifies the DOM element by using it’s the class name and returns the element.
Example:
<!DOCTYPE html><html><head><title>How to get/change the HTMLwithDOM elementinJavaScript?</title></head><body><h3>Accessing HTML of a DOMelementinJavaScript.</h3><p class="p1">JavaScript is usedforweb programming.</p><button onclick=getHtml()>Get and change the htmlforDOM element</button><script>functiongetHtml() {varElement =document.getElementsByClassName("p1");alert(Element[0].innerHTML);Element[0].style.color ="blue";Element[0].innerHTML ="GeeksforGeeks";}</script></body></html>chevron_rightfilter_noneOutput:
- Before Clicking the Button:

- After Clicking the Button:

- Before Clicking the Button:
- Using getElementsByTagName(): This method gets/identifies the DOM element using it’s tag name and returns it.
Example:
<!DOCTYPE html><html><head><title>How to get/change the HTMLwithDOM elementinJavaScript?</title></head><body><h3>Accessing HTML of a DOMelementinJavaScript.</h3><p>JavaScript is usedforweb programming.</p><p>JavaScript was inventedin1990s.</p><button onclick=getHtml()>Get and change the htmlforDOM element</button><script>functiongetHtml() {varElement = document.getElementsByTagName("p");for(vari = 0; i < Element.length; i++) {alert(Element[i].innerHTML);Element[i].innerHTML ="GeeksforGeeks.";}}</script></body></html>chevron_rightfilter_noneOutput:
- Before Clicking the button:

- After Clicking the Button:

- Before Clicking the button:
NOTE: The above three approaches use inner.HTML property of the DOM element to get the HTML and alerts it and then changes the HTML content present in the element. The property inner.HTML is mainly used to change the text or rather the content present whereas outer.HTML is used for changing the tag and the content of the text as it returns the HTML content along with its tag.
Below example illustrates the use of outer.HTML property using getElementsByTagName() method.
<!DOCTYPE html> <html> <head> <title> How to get/change the HTML with DOM element in JavaScript? </title> </head> <body> <div> GeeksforGeeks </div> <button onclick=getHtml()> Get HTML for DOM element </button> <script> function getHtml() { var Element = document.getElementsByTagName("div"); alert(Element[0].outerHTML); Element[0].style.color = "red"; Element[0].outerHTML = "<h1> JavaScript. </h1>" } </script> </body> </html> |
Output:
- Before Clicking the Button:

- After Clicking the Button:

The functions getElementById(), getElementsByClassName() can also be used to get the DOM elements for accessing outerHTML in the same manner as they were used to access innerHTML. Thus, we can access HTML of a DOM using the above methods.
Recommended Posts:
- Replace a DOM element with another DOM element in place
- JavaScript | Check if element exists in the visible DOM
- How to get the type of DOM element using JavaScript?
- How to add a class to DOM element in JavaScript?
- How to Linearly Transition the removal of a DOM element using CSS and JavaScript ?
- How to fade the removal of a DOM Element using CSS and JavaScript ?
- How to Remove HTML element from DOM using AngularJS ?
- HTML | DOM HTML Object
- Set the focus to HTML form element using JavaScript
- How to remove an HTML element using JavaScript ?
- Check whether HTML element has scrollbars using JavaScript
- How to add/update an attribute to an HTML element using JavaScript?
- How to rotate an HTML div element 90 degrees using JavaScript ?
- How to Hide an HTML Element by Class using JavaScript ?
- How to get the text without HTML element using JavaScript ?
- How to remove “disabled” attribute from HTML input element using JavaScript ?
- How to add an element horizontally in Html page using JavaScript?
- How to replace an HTML element with another one using JavaScript ?
- Remove all the child elements of a DOM node in JavaScript
- How to check a JavaScript Object is a DOM Object ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


