Ends in 0d:00h:00m:00s

Buy 1 year, 🎁 Gift 1 year — completely FREE

Split the cost with a friend. You both get 12 months for $99.

Start FREE trial
Background Image

Buy 1 year, Gift 1 year — completely FREE

Start FREE trial
Oussama EL ATTAOUI
PRO
last year
Oussamacountry asked

Whats the difference between innerHTML and outerHTML if both select the same HTML element?

Kelish Rai
Expert
last year
Kelish Rai answered

The difference between innerHTML and outerHTML is in what part of the element they work with.

  • innerHTML gives you just the content inside an element.

  • outerHTML gives you the entire element itself, including the tag.

Consider this code:

Hello

And this JavaScript:

const hero = document.querySelector(".demo");

Now if you do:

console.log(hero.innerHTML);

You'll get:

Hello

But if you do:

console.log(hero.outerHTML);

You'll get:

Hello

So, outerHTML includes the entire element, while innerHTML only includes what's inside it.

Simply put, use innerHTML if you want to change the content inside an element, and use outerHTML if you want to change the element itself.

This question was asked as part of the JavaScript in Browser course.