• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / HTML / outerHTML – How to Get and Change the outerHTML Property of an Element

outerHTML – How to Get and Change the outerHTML Property of an Element

August 6, 2022 Leave a Comment

We can get and change the outerHTML of an element using JavaScript. We can use the JavaScript getElementById() method to target the outerHTML property of an element.

document.getElementById("p1").outerHTML

Let’s see an example of this below.


Let’s say we have the following HTML:

<div id="div1">
  <p id="p1">This is a paragraph with some text.<p>
</div>

If we want to get the outerHTML of a paragraph with id, #p1, we can use our code from above to do this.

console.log(document.getElementById("p1").outerHTML);

#Output
//<p id="p1">This is a paragraph with some text.</p>

Let’s take a look at changing the outerHTML of an element using JavaScript in our next example.

Changing outerHTML of an Element using JavaScript

Changing the outerHTML of an element will essentially replace the element with whatever new HTML element you want.

Let’s take a look at some simple HTML code again:

<div id="div1">
  <p id="p1">This is our first paragraph with some text.<p>
  <p id="p2">This is our second paragraph with some text.<p>
  <p id="p7">This is a paragraph that we will want to change.<p>
  <div id="click-me" onclick="changeOuterHTML()">Change last pargraph</div>
</div>

In our HTML above, we will want to change the last paragraph with id #p7 and text “This is a paragraph that we will want to change.”. To do this, we will use the JavaScript getElementById() method to target that p element and target its outerHTML property. We will create a function that will run when the user clicks our button to run this code.

Here is our JavaScript code to change the outerHTML of an element:


document.getElementById("p7").outerHTML = "<p id='p3'>This is our third paragraph with some text.<p>";

And here is the JavaScript code in our function:

function changeOuterHTML(){
  document.getElementById("p7").outerHTML = "<p id='p3'>This is our third paragraph with some text.<p>";
};

The final code and output for this example of how to change the outerHTML of an element using Javascript is below:

Code Output:

This is our first paragraph with some text.

This is our second paragraph with some text.

This is a paragraph that we will want to change.

Change last pargraph

Full Code:

<div id="div1">
  <p id="p1">This is our first paragraph with some text.<p>
  <p id="p2">This is our second paragraph with some text.<p>
  <p id="p7">This is a paragraph that we will want to change.<p>
  <div id="click-me" onclick="changeOuterHTML()">Change last pargraph</div>
</div>

<script>

function changeOuterHTML(){
  document.getElementById("p7").outerHTML = "<p id='p3'>This is our third paragraph with some text.<p>";
};

</script>

Hopefully this article has been useful for you to understand how to get and change the outerHTML of an element using JavaScript.

Other Articles You'll Also Like:

  • 1.  How to Use JavaScript to Round Up Numbers
  • 2.  How to Generate a Random Letter In JavaScript
  • 3.  HTML span width – Set the Width of a Span in HTML
  • 4.  Push Multiple Items to Array in JavaScript
  • 5.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 6.  How to Change the Id of an Element in JavaScript
  • 7.  Remove Word From String In JavaScript
  • 8.  JavaScript tanh – Find Hyperbolic Tangent of Number Using Math.tanh()
  • 9.  JavaScript Random Number – How to Generate a Random Number In JavaScript
  • 10.  Using JavaScript to Redirect to a Relative URL

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy