Vanilla JavaScript approach:
In this scenario, the usage of getElementById()
is not suitable as it is specifically designed to select elements by their id attributes. Instead, you can employ getElementsByTagName()
within the context of #menu
:
var menu = document.getElementById('menu');
// Retrieve all <li> child elements of #menu
var listItems = menu.getElementsByTagName('li');
// Iterate through each one
for (var i=0; i<listItems.length; i++) {
// Obtain all <a> child elements of every <li>
var anchorTags = listItems[i].getElementsByTagName('a');
for (var j = 0; j<anchorTags.length; j++) {
// Update their color dynamically
anchorTags[j].style.color = 'blue';
// or modify other CSS properties
anchorTags[j].style.height = '25%';
}
}
Solution using jQuery:
If jQuery is accessible to you, achieving this task becomes much simpler:
$('#menu li a').css('color', 'blue');