How to get/change the HTML with DOM element in JavaScript ?
Last Updated :
12 Jul, 2025
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 the getElementById() method: This method gets/identifies the DOM elements using its ID and returns the element.
Example: In this example, we will use the getElementById() method of javascript to change the HTML DOM element in javascript.
JavaScript
<!DOCTYPE html>
<html>
<head>
<title>
How to get/change the HTML with
DOM element in JavaScript?
</title>
</head>
<body>
<h3>
Accessing HTML of a DOM
element in JavaScript.
</h3>
<p id="iD1">
JavaScript is used for web programming.
</p>
<button onclick=getHtml()>
Get and change the html for DOM element
</button>
<script>
function getHtml() {
var Element = document.getElementById("iD1");
alert(Element.innerHTML);
Element.style.color = "orange";
Element.innerHTML = "GeeksforGeeks";
}
</script>
</body>
</html>
Output:
How to get/change the HTML with DOM element in JavaScript ?
Using the getElementByName() method: This method gets/identifies the DOM element by using its class name and returns the element.
Example: In this example, we will use the getElementsByName() method of javascript to change the HTML DOM element in javascript.
JavaScript
<!DOCTYPE html>
<html>
<head>
<title>
How to get/change the HTML with
DOM element in JavaScript?
</title>
</head>
<body>
<h3>
Accessing HTML of a DOM
element in JavaScript.
</h3>
<p class="p1">
JavaScript is used for web programming.
</p>
<button onclick=getHtml()>
Get and change the html
for DOM element
</button>
<script>
function getHtml() {
var Element =
document.getElementsByClassName("p1");
alert(Element[0].innerHTML);
Element[0].style.color = "blue";
Element[0].innerHTML = "GeeksforGeeks";
}
</script>
</body>
</html>
Output:
How to get/change the HTML with DOM element in JavaScript ?
Using the getElementsByTagName(): This method gets/identifies the DOM element using its tag name and returns it.
Example: In this example, we will use the getElementsByTagName() method of javascript to change the HTML DOM element in javascript.
JavaScript
<!DOCTYPE html>
<html>
<head>
<title>
How to get/change the HTML with
DOM element in JavaScript?
</title>
</head>
<body>
<h3>
Accessing HTML of a DOM
element in JavaScript.
</h3>
<p>JavaScript is used for web programming.</p>
<p>JavaScript was invented in 1990s.</p>
<button onclick=getHtml()>
Get and change the html for DOM element
</button>
<script>
function getHtml() {
var Element = document.getElementsByTagName("p");
for (var i = 0; i < Element.length; i++) {
alert(Element[i].innerHTML);
Element[i].innerHTML = "GeeksforGeeks.";
}
}
</script>
</body>
</html>
Output:
How to get/change the HTML with DOM element in JavaScript ?
NOTE: The above three approaches use the inner.HTML property of the DOM element to get the HTML and alert it and then change 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.
The below example illustrates the use of outer.HTML property using the getElementsByTagName() method.
JavaScript
<!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:
How to get/change the HTML with DOM element in JavaScript ?
The functions getElementById(), and 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 the HTML of a DOM using the above methods.
Similar Reads
How to Change the ID of Element using JavaScript? We are given an element and the task is to change the ID of elements using JavaScript. ID is unique for any element and it can be assigned to an element only once. JavaScript provides a method to access this id and also to manipulate the id. Syntax:Selected_element.id = newID;Below are the appraoche
2 min read
How to Add JavaScript in HTML Document? To add JavaScript in HTML document, several methods can be used. These methods include embedding JavaScript directly within the HTML file or linking an external JavaScript file.Inline JavaScriptYou can write JavaScript code directly inside the HTML element using the onclick, onmouseover, or other ev
3 min read
How to select DOM Elements in JavaScript ? Selecting DOM (Document Object Model) elements is a fundamental aspect of web development with JavaScript. It allows developers to interact with and manipulate elements on a webpage dynamically. Proper selection of elements is crucial for tasks such as updating content, adding event listeners, or mo
3 min read
How to replace an HTML element with another one using JavaScript? Replacing an HTML element with another using JavaScript allows for dynamic modification of webpage content without reloading the entire page. Document Object Model (DOM) is a platform and language-neutral interface that is used by programs and scripts to dynamically access the content, style, and st
2 min read
How to Select an Element by ID in JavaScript ? In JavaScript, we can use the "id" attribute to interact with HTML elements. The "id" attribute in HTML assigns a unique identifier to an element. This uniqueness makes it easy for JavaScript to precisely target and manipulate specific elements on a webpage. Selecting elements by ID helps in dynamic
2 min read
How to get the outer html of an element using jQuery ? Sometimes, there is a need to get the entire HTML element by its id and not merely its contents, for doing so, we shall use the HTML DOM outerHTML Property to get the outer HTML of HTML element. Syntax: document.getElementById("your-element-id").outerHTML) You can use a variable and initialize it to
2 min read