Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: get element by class

  1. #1
    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    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!

  4. #4
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks twey, that is excellent!

    Agrajag

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    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

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    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.
    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!

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    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

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    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!

  9. #9
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    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

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •