Oussama
asked

Expert
Kelish Rai answered
The difference between innerHTML and outerHTML is in what part of the element they work with.
innerHTMLgives you just the content inside an element.outerHTMLgives you the entire element itself, including the tag.
Consider this code:
<div class="demo"><p>Hello</p></div>And this JavaScript:
const hero = document.querySelector(".demo");Now if you do:
console.log(hero.innerHTML);You'll get:
<p>Hello</p>But if you do:
console.log(hero.outerHTML);You'll get:
<div id="demo"><p>Hello</p></div>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.
Our Experts
Sanjeev PanditPrincipal Engineer
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Anuj ShresthaTech Lead
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer