Image

Imagejuly4th wrote in Imagejavascript

Dynamically adding events

I'm having trouble dynamically adding events to elements in Internet Explorer 6.0.

The following works like a charm in Firefox, but does absolutely nothing in IE. And by nothing, I mean nuh-thing. Not even a javascript warning:

document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
}

function show_date() {
    this.childNodes[1].style.display = "inline";
}

function hide_date() {
    this.childNodes[1].style.display = "none";
}

function add_function() {
    var grid = document.getElementsByClassName('date');
  
    for (var i = 0; i < grid.length; i++) {
        if (window.addEventListener) {
            grid[i].addEventListener("mouseover",show_date, false);
            grid[i].addEventListener("mouseout",hide_date, false);
        } else {
            grid[i].attachEvent("mouseover",show_date, false);
            grid[i].attachEvent("mouseout",hide_date, false);
        }
    }
}

Any thoughts?