Reputation:
Why nesting getElementById throwing error? fiddle how can i solve this
var links = document.getElementById('menu').getElementById('one');
links.style.color="red";
but getElementsByTagName is working. fiddle
var links = document.getElementById('menu').getElementsByTagName('li');
links[0].style.color="red";
please explain why?
helps will be appreciated
Upvotes: 0
Views: 193
Reputation: 943694
An id
must be unique in a document. A tag name doesn't.
The only reason to try to call getElementById
on a specific element is to find out if the element with the given ID exists as a descendant of a specific other element, but that wasn't something that was considered during the design of the API.
You can use querySelector('#someId')
to achieve that.
Upvotes: 3