Olivier
05-11-2005, 09:56 PM
Hello all ;)
A post title not very clear, but I don't know how make it better... anyway,
my problem is simple, the solution certainly not ;)
I have made a method .getElementsByClassName here :
document.getElementsByClassName = function(className) {
var elts = document.getElementsByTagName('*');
var classArray = new Array();
for (var i = 0; i < elts.length; ++i) {
if (elts[i].getAttribute('class') && elts[i].getAttribute('class').split(' ').inArray(className)) {
classArray.push(elts[i]);
}
}
return classArray;
};
Okay, fine, it works, but only for document.getElementsByClassName, not for obj.getElementsByClassName where obj is for example document.getElementById('anId')
Is it possible to make the method work with this ??
A little question more :
Is it possible to make this line
var elts = document.getElementsByTagName('*');
less long, a short way to do the same thing ?
I see this on http://www.gazingus.org/html/Flexible_Node_Retrieval.html
If no filter function is specified, all nodes of the given type are returned. For the Node.ELEMENT_NODE type, this is the same as calling node.getElementsByTagName("*"). Of course, you would not want to do this since the built-in function is more efficient.
But it doesn't work :/
Thanks ;)
A post title not very clear, but I don't know how make it better... anyway,
my problem is simple, the solution certainly not ;)
I have made a method .getElementsByClassName here :
document.getElementsByClassName = function(className) {
var elts = document.getElementsByTagName('*');
var classArray = new Array();
for (var i = 0; i < elts.length; ++i) {
if (elts[i].getAttribute('class') && elts[i].getAttribute('class').split(' ').inArray(className)) {
classArray.push(elts[i]);
}
}
return classArray;
};
Okay, fine, it works, but only for document.getElementsByClassName, not for obj.getElementsByClassName where obj is for example document.getElementById('anId')
Is it possible to make the method work with this ??
A little question more :
Is it possible to make this line
var elts = document.getElementsByTagName('*');
less long, a short way to do the same thing ?
I see this on http://www.gazingus.org/html/Flexible_Node_Retrieval.html
If no filter function is specified, all nodes of the given type are returned. For the Node.ELEMENT_NODE type, this is the same as calling node.getElementsByTagName("*"). Of course, you would not want to do this since the built-in function is more efficient.
But it doesn't work :/
Thanks ;)