InnerText vs TextContent Property in Javascript

Th key difference sample code between innertext and textcontent property in javascript.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div>
Hello World
<span>This is hidden</span>
</div>
<button>Click Here</button>
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div> Hello World <span>This is hidden</span> </div> <button>Click Here</button> <script src="script.js"></script> </body> </html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    
    <div>
        Hello World

        <span>This is hidden</span>
    </div>

    <button>Click Here</button>

    <script src="script.js"></script>
</body>
</html>

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
document.querySelector('button').addEventListener('click', ()=>{
const result = document.querySelector('div').textContent;
console.log(result);
});
document.querySelector('button').addEventListener('click', ()=>{ const result = document.querySelector('div').textContent; console.log(result); });
document.querySelector('button').addEventListener('click', ()=>{
    const result = document.querySelector('div').textContent;

    console.log(result);
});

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
div {
width: 100%;
background-color: maroon;
text-align: center;
padding: 10px;
margin-bottom: 15px;
color: #fff;
}
span {
display:none;
}
div { width: 100%; background-color: maroon; text-align: center; padding: 10px; margin-bottom: 15px; color: #fff; } span { display:none; }
div {
    width: 100%;
    background-color: maroon;
    text-align: center;
    padding: 10px;
    margin-bottom: 15px;
    color: #fff;
}

span {
    display:none;
}