I am doing XML parsing and come upon a weird problem. The getElementsByTagName from a DOM element works, but getElementById doesn't. Chrome gives this error anywhere I call getElementById for the manifest:
Object #<Element> has no method 'getElementById'
But somehow getElementByTagName works just fine.
var manifest = content.getElementsByTagName("manifest")[0];
var spine = content.getElementsByTagName("spine")[0];
var itemref = spine.getElementsByTagName("itemref")[0].getAttribute("idref");
alert(itemref);//works
var firstpage = manifest.getElementById(itemref).getAttribite("href");
Can anyone please explain to me why the getElementById DOM is not working for me?
test = manifest.getElementById("any value");//Problem
test = manifest.getElementsByTagName("any value");//no Problem
You can't call getElementById
as a method of arbitrary elements, it is only available on the document
element. That makes sense, as there shouldn't be any duplicate IDs on an HTML document.
That being said, your call getElementById("a")
looks strange; do you really have an element with id="a"
, or are you trying to get a reference to an arbitrary anchor?