Blog>
Snippets

Selecting elements by tag name

Use `getElementsByTagName` to select all `<p>` elements and change their text color.
var paragraphs = document.getElementsByTagName('p');
for (var i = 0; i < paragraphs.length; i++) {
    paragraphs[i].style.color = 'red';
}
This code snippet selects all paragraph elements ('<p>') in the document using `getElementsByTagName` and changes their text color to red. It iterates over the NodeList of paragraphs and applies the style change to each element.