Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>querySelectorAll Example</title> </head> <body> <div class="example">Element 1</div> <div class="example">Element 2</div> <button onclick="highlightAll()">Highlight All</button> <script> function highlightAll() { const elements = document.querySelectorAll('.example'); elements.forEach((element, index) => { element.style.backgroundColor = 'lightgreen'; element.textContent = `Element ${index + 1} highlighted!`; }); } </script> </body> </html>