June 14, 2019
InnerText vs TextContent Property in Javascript
<!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>
document.querySelector('button').addEventListener('click', ()=>{ const result = document.querySelector('div').textContent; console.log(result); });
div { width: 100%; background-color: maroon; text-align: center; padding: 10px; margin-bottom: 15px; color: #fff; } span { display:none; }