With the code from this article I've successfully added querySelectorAll
to document
in IE7.
But I need to use it on an element rather than document
, like this:
var containers = document.querySelectorAll('.container'); // Works
for (var i=0; i<=containers.length; i++) {
var container = containers[i];
container.querySelectorAll('a[data-type="people"]'); // Fails
// ...
}
Is there a way to add querySelectorAll
to elements in IE7 rather than only to document
?
Very interesting question.
I would lean toward using a library for this, like jQuery, one of the ones mentioned below, Closure, or any of several others. Or just using Sizzle (the selector engine jQuery uses in the v1.9 branch).
But answering the question you actually asked:
You can't extend element prototypes on IE7 (sadly), so unless you want to run all of your element instances through a preprocessor function to add
querySelectorAll
to them (the way Prototype and MooTools work, for instance), you'll have to have a separate function you pass the element into.Here's one approach to writing that function:
And of course, if you want to use
qsaWorker
in browsers that havequerySelectorAll
, you'd want this:So in total, then, you'd probably want:
Your code using it would then look like this:
But again, I would tend to lean toward using a library...