Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Select Elements by Tag Name</title> </head> <body> <p>This is a paragraph of text.</p> <div class="test">This is another paragraph of text.</div> <p>This is one more paragraph of text.</p> <hr> <script> // Selecting all paragraph elements let matches = document.getElementsByTagName("p"); // Printing the number of selected paragraphs document.write("Number of selected elements: " + matches.length); // Highlighting each paragraph's background through loop for(let elem in matches) { matches[elem].style.background = "yellow"; } </script> </body> </html>