The answer is because getElementsByClassName()
and getElementById()
don't take wildcards. They aren't spec'ed or written to do that so you can't use them that way.
Further getElementById()
only returns a single DOM element, not an array of elements so it could never work that way.
If you wanted to get all elements with a class name or all elements with an id, you could do so like this:
function getAllElementsWithAttribute(attr) {
var results = [];
var all = document.getElementsByTagName("*");
for (var i = 0, len = all.length; i < len; i++) {
if (all[i][attr]) {
results.push(all[i]);
}
}
return(results);
}
You could then get all elements with a class name with this:
var items = getAllElementsWithAttribute("className");
Or, get all elements with an ID with this:
var items = getAllElementsWithAttribute("id");