

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
innerHTML vs innerText in JavaScript.
innerHTML − The innerHTML property returns the text, including all spacing and inner element tags. It preserves the formatting of the text and all the extra tags like <b>, <i> etc.
innerText − The innerText property returns just the text, removing the spacing and the inner element tags.
Following is the code for innerHTML and innerText in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 18px; font-weight: 500; color: red; } </style> </head> <body> <h1>innerHTML vs innerText</h1> <div class="sample">Hello <b>World</b> <i>Some Text</i></div> <div class="result">innerHTML =</div> <div class="result">innerText =</div> <button class="Btn">Click here</button> <h3>Click on the above button to see the innerText and innerHTML result for the above paragraph</h3> <script> let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelectorAll(".result"); let sampleEle = document.querySelector(".sample"); let a; BtnEle.addEventListener("click", () => { resEle[0].innerHTML += sampleEle.innerHTML; resEle[1].innerHTML += sampleEle.innerText; }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Questions & Answers
- How can I get H1 innerText in JavaScript without the innerText of its child?
- Set the innerHTML with JavaScript
- Disadvantages of using innerHTML in JavaScript
- HTML DOM innerText Property
- HTML DOM innerHTML Property
- How to get the innerHTML of a cell with JavaScript DOM?
- Push() vs unshift() in JavaScript?
- \d vs \D in JavaScript?
- Window.onload vs onDocumentReady in javascript
- Const vs Let in JavaScript.
- Undeclared vs Undefined? In JavaScript
- Arrays vs Set in JavaScript.
- Clicking an element using javascript vs actions vs webdriver?
- JavaScript function in href vs. onClick
- Shift() vs pop() methods in JavaScript?
Advertisements