Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-16-2006, 04:42 AM
Agrajag Agrajag is offline
Junior Coders
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default get element by class

how can i use a function to get elements by their className?

thanks
Reply With Quote
  #2  
Old 05-16-2006, 05:03 AM
jscheuer1's Avatar
jscheuer1 jscheuer1 is offline
No Kidding?
 
Join Date: Mar 2005
Location: SE PA USA
Posts: 18,999
Thanks: 19
Thanked 1,135 Times in 1,121 Posts
Blog Entries: 3
Default

Here's one from the Switch Content II script in the DD Library:

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
}
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.
__________________
WWWWWWWWWWWW
- John
________________________

Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Reply With Quote
  #3  
Old 05-16-2006, 07:31 AM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

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!
Reply With Quote
  #4  
Old 05-16-2006, 11:08 AM
Agrajag Agrajag is offline
Junior Coders
 
Join Date: Jan 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks twey, that is excellent!

Agrajag
Reply With Quote
  #5  
Old 05-16-2006, 12:18 PM
mwinter mwinter is offline
Elite Coders
 
Join Date: Dec 2004
Location: UK
Posts: 2,361
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Twey
I've always used this one.
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.

The Array.prototype.push method substitute is bogus, though.

Mike
Reply With Quote
  #6  
Old 05-16-2006, 12:25 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

The IE5-support one?
Does IE5 already have one? Perhaps
Code:
if(!Array.push) Array.prototype.push = function(val) {
      this[this.length] = value;
  }
would be better anyway.
Quote:
Any code that declares itself to be 'the ultimate' requires reviewing.
Heh, yes. I did have a check through it before using it, though.
__________________
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!
Reply With Quote
  #7  
Old 05-16-2006, 12:31 PM
mwinter mwinter is offline
Elite Coders
 
Join Date: Dec 2004
Location: UK
Posts: 2,361
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Twey
The IE5-support one?
Does IE5 already have one?
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).

Mike
Reply With Quote
  #8  
Old 05-16-2006, 12:38 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

Quote:
Originally Posted by Mike@robertnyman.com
Code:
if (typeof Array.prototype.push != 'function') {
  Array.prototype.push = function(v) {
    var i = this.length >>> 0,
        j = 0;

    while(j < arguments.length) {this[i++] = arguments[j++];}
    return this.length = i;
  };
}
Only one minor, pedantic qualm: you've defined v but never used it 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!
Reply With Quote
  #9  
Old 05-16-2006, 12:40 PM
mwinter mwinter is offline
Elite Coders
 
Join Date: Dec 2004
Location: UK
Posts: 2,361
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Twey
Only one minor, pedantic qualm: you've defined v but never used it
Intentional. 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
Reply With Quote
  #10  
Old 05-16-2006, 12:44 PM
Twey's Avatar
Twey Twey is offline
Modtoreador
 
Join Date: Jun 2005
Location: 英国
Posts: 11,933
Thanks: 1
Thanked 180 Times in 172 Posts
Blog Entries: 2
Default

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!
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:56 PM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.