June 14, 2019
InnerText vs TextContent Property in Javascript
In this video, you will learn what is the key difference between innertext and textcontent property in javascript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!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> |
1 2 3 4 5 |
document.querySelector('button').addEventListener('click', ()=>{ const result = document.querySelector('div').textContent; console.log(result); }); |
1 2 3 4 5 6 7 8 9 10 11 12 |
div { width: 100%; background-color: maroon; text-align: center; padding: 10px; margin-bottom: 15px; color: #fff; } span { display:none; } |