Moose Moose - 1 year ago 64
Javascript Question

"getElementById not a function" when trying to parse an AJAX response?

I'm running

(in a Greasemonkey script) and storing the
into a newly created HTML element:

var responseHTML = document.createElement('HTML');
onload: function() { responseHTML.innerHTML = response.responseText; }

And then I am trying to find an element in


The first works fine, but not the second. Any ideas?

Answer Source

getElementById is not a method of HTML elements. It is a method of the document node. As such you can't do:

div.getElementById('foo'); // invalid code

You can implement your own function to search the DOM by recursively going through children. On newer browsers you can even use the querySelector method. For minimal development you can use libraries like jQuery or sizzle.js (the query engine behind jQuery).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download