how can i use a function to get elements by their className?
thanks
how can i use a function to get elements by their className?
thanks
Here's one from the Switch Content II script in the DD Library:
You call it with the two parameters, the first param is the root object that the elements with your desired class designation are contained in, the second param is the class name you are looking for. It returns an array containing all children of the root object with that class name.Code:function getElementbyClass(rootobj, classname){ var temparray=new Array() var inc=0 var rootlength=rootobj.length for (i=0; i<rootlength; i++){ if (rootobj[i].className==classname) temparray[inc++]=rootobj[i] } return temparray }
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
I've always used this one.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
thanks twey, that is excellent!
Agrajag
Any code that declares itself to be 'the ultimate' requires reviewing. It's fine, though as commented by other reviewers, escaping hyphens is totally unnecessary.Originally Posted by Twey
The Array.prototype.push method substitute is bogus, though.
Mike
The IE5-support one?
Does IE5 already have one? Perhapswould be better anyway.Code:if(!Array.push) Array.prototype.push = function(val) { this[this.length] = value; }Heh, yes. I did have a check through it before using it, though.Any code that declares itself to be 'the ultimate' requires reviewing.![]()
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
No, the reason for it is valid: prior to JScript 5.5, there was no push method implemention (though it was specified and implemented elsewhere). The suggested implementation is just bad. I added a comment (twice; it barfed on <, unlike here in DDF).Originally Posted by Twey
Mike
Only one minor, pedantic qualm: you've defined v but never used itOriginally Posted by Mike@robertnyman.com
I seem to remember you doing something like this before, also with a argument named "v," as I recall.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Intentional.Originally Posted by Twey
The Array.prototype.push method is defined as having a length property value of 1. For user-defined functions, the only way to affect the length property is via the formal argument list; the property itself is read only.
Mike
Aha, I see.
Qualm dismissed.![]()
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks