PDA

View Full Version : [solved] getElementsByClassName for element (not document)


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 ;)

MikeFoster
05-11-2005, 10:17 PM
Hi Olivier,

I've written one of those too (I guess we all have :)). Have a look: xGetElementsByClassName (http://cross-browser.com/x/lib/index.php?sym=xgetelementsbyclassname)

Olivier
05-11-2005, 10:28 PM
Yes, but this is a function version, not method version :p

I had already done the same thing or quite the same, but for the beauty of the thing I want make it with a method (lol), seriously, I want to make this with a method in order to approch getElementById, getElementsByTagName natives methods and because I feel this way more logical and practice.

That's why I search to make my actual version better...

But, I had already a function version who work for anything ;) thanks for your answer !

jkd
05-11-2005, 10:50 PM
HTMLElement.prototype.getElementsByClassName = function(className) {
var elements = this.getElementsByTagName("*");
// blabla
}


Gecko-only.

MikeFoster
05-11-2005, 11:50 PM
There's more than one way to skin a cat... especially in the summer time :p LOL!

var list = document.getElementsByTagName('*');
for (var i = 0; i < list.length; ++i) {
list[i].getElementsByClassName = my_sillyness;
}
function my_sillyness(tag)
{
return xGetElementsByClassName(tag, this);
}

jkd
05-11-2005, 11:59 PM
But that only sets the methods for elements physically in the source. When content is inserted at runtime via Javascript DOM manipulation, they will be lacking the method.

Olivier
05-12-2005, 12:13 AM
HTMLElement.prototype.getElementsByClassName = function(className) {
var elements = this.getElementsByTagName("*");
// blabla
}


Gecko-only.


Yes, I had already tried this solution before my post, but some problem :
* first, It doesn't work on IE (opera ?)
* second, it doesn't work as I think it could work, for example, document.getElementById('myId').getElementsByClassName('myClass'); will match all elements where class contain myClass and not only the elements contained in eement myId :/ But it works like document.getElementsByClassName

Your last message meens that it isn't a solution for this problem ?
Does it (DOM created elements) work with the document.getElementsByClassName method ?

Thanks for your answers !

MikeFoster
05-12-2005, 04:49 AM
But that only sets the methods for elements physically in the source. When content is inserted at runtime via Javascript DOM manipulation, they will be lacking the method.

Yep. You're right :)

liorean
05-12-2005, 05:24 AM
The solution for all cases like this is to instead of trying to get object.method(arguments) to work, use function(object,arguments).

It's not as nice from an object orientation perspective, but unlike the alternatives it works.

Olivier
05-12-2005, 08:36 AM
The solution for all cases like this is to instead of trying to get object.method(arguments) to work, use function(object,arguments).

It's not as nice from an object orientation perspective, but unlike the alternatives it works.

Yes, it seems that's the solution...

Thanks a lots guys ;)

See you soon !

Olivier
05-18-2005, 11:21 AM
try object instead of doument!
Okay, I will try ;)

After my exams :p don't worry if I don't answer this topic fastly ;)

Thanks !