Get the text content of an element:
var x = document.getElementById("myBtn").textContent;
Click the button get the text content of the button element.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()" id="myBtn">Test</button> <p id="demo"></p> <script> function myFunction() {//from w w w. j a v a 2s .co m var x = document.getElementById("myBtn").textContent; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The textContent property sets or gets the text content of the specified node, and all its descendants.
Setting the textContent property will replace any child nodes by a single Text node containing the specified string.
To set or return the HTML content of an element, use the innerHTML property.
The textContent property returns null if the element is a document, a document type, or a notation.