SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementsByClassName not working in IE?

    Hi There, i'm a javascript newbie. Based on the book "Simply Javascript", i can't seem to get the getElementsByClassName function to work on IE. I don't know why.

    There is no error on IE. I've tried with IE Tester and i get this error:

    Object doesn't support this property or method
    Additional Note: This works on Firefox and Chrome

    Code JavaScript:
    function getElementsByClassName(className) {// Get a list of all elements in the document
        // For IE    
        if (document.all) {
            var allElements = document.all;
        } else {
            var allElements = document.getElementsByTagName("*");
        }
     
        // Empty placeholder to put in the found elements with the class name
        var foundElements = [];  
     
        for (var i = 0, ii = allElements.length; i < ii; i++) {
            if (allElements[i].className == className) {
                foundElements[foundElements.length] = allElements[i];
            }
        }
     
        return foundElements;
     
    }
     
    // Getting an item by class name
     
    var listItem = document.getElementsByClassName("quirky");
     
    for (var i = 0, ii = listItem.length; i < ii; i++) {
        alert(listItem[i].nodeName);
    }

    Thank you for reading this. Any help is greatly appreciated
    Jaysone

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kennethjaysone View Post
    Hi There, i'm a javascript newbie. Based on the book "Simply Javascript", i can't seem to get the getElementsByClassName function to work on IE.
    The problem is with this line here:

    Code:
    document.getElementsByClassName("quirky")
    That line is not using the function you created. Instead it is trying to use the built-in method that the web browser has. IE doesn't have it, so IE complains.

    You can fix that by checking if the browser implements document.getElementsByClassName. If it doesn't, add it.

    Code javascript:
    if (!document.getElementsByClassName) {
        document.getElementsByClassName = function (className) {
            // contents of the function here
        }
    }

    I still don't know how to get IE to use prototypal inheritance so that you can a similar technique from any element, but at least the above will work when you use document.getElementsByClassName(...)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

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
  •