Source Code:
(back to article)
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> .text-class { color: blue; } </style> </head> <body> <h2 class="text-class">Welcome to W3Docs!</h2> <h2 id="text"></h2> <button id="w3Button" type="button" onclick="clickFunction()">Click on button</button> <script> function clickFunction() { // Select element and get attribute let button = document.getElementsByTagName("h2")[0].getAttribute("class"); document.getElementById("text").innerHTML = button; } </script> </body> </html>
Result:
Report an issue