how to extend document.getElementsBy*() in Firefox  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | |
I want to add (prototype) a method to the node list returned by document.getElementsByName() (and related, i.e. NodeList resp. HTMLCollection).
unfortunately, neither the NodeList nor the HTMLCollection interface seem to be prototypable in Firefox. (NodeList works in Safari & Opera, don't know about IE)
has anyone of you an idea how to solve this?
note: this matter has already been discussed, though I didn't find any solution to this problem. (beside prototyping into Object interface).
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: how to extend document.getElementsBy*() in Firefox
I don't think this is possible. Either create a function which does the job or create your own object with the nodelist and then extend that instead.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: how to extend document.getElementsBy*() in Firefox
you mean substituting document.getElementsBy*() with my own function (kind of)?
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: how to extend document.getElementsBy*() in Firefox
may be you could live with something like that: - document.baseGetElementsByTagName = document.getElementsByTagName;
-
-
document.getElementsByTagName = function(n) {
-
top.console.log(this.baseGetElementsByTagName(n));
-
}
-
-
document.getElementsByTagName('div');
kind regards
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: how to extend document.getElementsBy*() in Firefox Quote:
Originally Posted by Dormilich you mean substituting document.getElementsBy*() with my own function (kind of)? Not necessarily. You could have a function which does whatever you want to do with the node-list and pass it the result of document.getElementsBy*(). If that's not to your liking, create your own object and pass it the node-list and extend to your heart's content.
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: how to extend document.getElementsBy*() in Firefox
@gits: that won't do, since I don't want to do that for every method that's returning a HTMLCollection.
@acoder: passing the NodeList as argument seems indeed the best way.
maybe I'll do both in a try-catch block…
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: how to extend document.getElementsBy*() in Firefox
hmmm ... what about that simple loop for that task? :) (FF + Firebug to test in console) - function extendMethod(fn) {
-
top.console.log(fn);
-
-
document['base' + fn] = document[fn];
-
-
document[fn] = function(n) {
-
nodeList = this['base' + fn](n);
-
nodeList = doSomethingWithNodeList(nodeList);
-
-
return nodeList;
-
};
-
}
-
-
for (var i in document) {
-
if (/getElements/.test(i)) {
-
extendMethod(i);
-
}
-
}
-
-
function doSomethingWithNodeList(nl) {
-
top.console.log(nl);
-
}
-
-
document.getElementsByClassName('normal');
-
that would always pass the nodelist for all 'getElements'-methods to a function called 'doSomethingWithNodeList' ... before the 'getElements'-method returns
kind regards
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: how to extend document.getElementsBy*() in Firefox
I have to bear that in mind, maybe I can use it sometimes…
that's what I want to do: - document.getElementsByClassName("attachEvent").addEventForEach("click", doSomething, false, some_param);
obviously the initial thought was to extend HTMLCollection or NodeList. but alas, I don't want to call a function (i.e. attach an event) every time I get a HTMLCollection from the document. seems like I have to use a function, where I pass the NodeList…
EDIT: hm, maybe I can do something with prototype's bind() method…
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: how to extend document.getElementsBy*() in Firefox
the construct does that exactly? here is just a litle adaption to have it a bit more generic :) - function extendMethod(fn) {
-
top.console.log(fn);
-
-
document['base' + fn] = document[fn];
-
-
document[fn] = function(n, eachFn) {
-
nodeList = this['base' + fn](n);
-
nodeList = eachFn(nodeList);
-
-
return nodeList;
-
};
-
}
-
-
for (var i in document) {
-
if (/getElements/.test(i)) {
-
extendMethod(i);
-
}
-
}
-
-
document.getElementsByClassName('normal', function(n) {
-
for (var i in n) top.console.log(n[i]);
-
});
-
this expl. code could just simply integrated into any oo framework etc. and now it' s able to get an eachFn param passed to the getElements-method ... that is a function that should be called with every node of the returned nodelist.
kind regards
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: how to extend document.getElementsBy*() in Firefox Quote:
Originally Posted by gits now it' s able to get an eachFn param passed to the getElements-method in the end it's like calling eachFn(nodeList) inside the getElementsBy*() method. so I just moved the function call from outside to inside… well, for my current problem this rather seems not worth it.
thanks anyway for the insight into method manipulation.
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany
Posts: 4,136
| | | re: how to extend document.getElementsBy*() in Firefox
:) nothing to thank for ... i currently think about such things too since you asked that question ... and for our current goal those ideas above might help us sometimes ;) ...
kind regards
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,660
| | | re: how to extend document.getElementsBy*() in Firefox
found some talk about DOM iteration 1, 2. this could be a useful starting point.
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|