I can’t get this to work:
<p>
<a href="#">First</a>
<a href="#" id="hasID">Second</a>
<a href="#">Third</a>
<a href="#" id="someID">Fourth</a>
</p>
<script>
var link = document.getElementsByTagName("a");
link.style.fontSize = '16px';
link.style.textDecoration = 'none';
link.style.color = '#333333';
link.style.fontWeight = 'bold';
</script>
I’m trying to add CSS styles (font-size, text-decoration, color, font-weight) to all the <a> tags of my HTML code.
This isn’t working because you’re trying to apply the changes to the list vs. the individual links. You need to loop through the links and apply the changes to the individual items
Additionally it looks like your script is running before the
aelements are defined. Hence thegetElementsByTagNamewill return an empty collection. Try moving the script to after the definition of the anchor elements