Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>getElementsByClassName Example</title> </head> <body> <div class="example">Element 1</div> <div class="example">Element 2</div> <button onclick="changeContent()">Change Content</button> <script> function changeContent() { const elements = document.getElementsByClassName('example'); Array.from(elements).forEach((element, index) => { element.textContent = `Element ${index + 1} changed!`; }); } </script> </body> </html>