Search code examples
javascriptinternet-explorermouseeventonmouseover

onmouseover for the last of several generated divs will not work in IE


I have a JavaScript function:

function addTool(id, text, tool, pic) {
   var container = getById('infobox');
   var origimg = getById('tempimg').src;
   container.innerHTML += "<div id='" + id + "' class='toolText'>" + text + "<br><img      class='toolImg' src='img/tools/" + tool + "'></div>";
   getById(id).setAttribute('onMouseOver', "mOver('"+ id +"', '" + pic + "');");
   getById(id).setAttribute('onMouseOut', "mOut('"+ id +"', '" + origimg + "');");
   getById(id).setAttribute('href', 'javascript:mClick(id);');
}

Which generates several divs, using this code:

addTool("1p", "Bar", "tool1.jpg", 'img/p&g-part-2_skiss1-2.jpg');
addTool("2p", "Tube", "tool1.jpg", 'img/p&g-part-2_skiss1-2.jpg');
addTool("3p", "Rotating", "tool1.jpg", 'img/p&g-part-2_skiss1-2.jpg');

The mouse events work fine in all major browsers except IE. It seems that all divs except the last will have the mouse event in lowercase which will have the mouse event exactly as written, with upper case letters.
All mouse events will fire except for the last div, even if I write onmouseover instead of say ONmouseOVER, which works fine on all except the last.


Solution

  • Just as @epascarello pointed out, it seems that the setAttribute was the culprit, so I resolved it by setting the events in inline, such as this:

    function addTool(id, text, tool, pic) {
        var container = getById('infobox');
        var origimg = getById('tempimg').src;
        container.innerHTML += "<div id='" + id + "' class='toolText'"  +
        "onmouseover=\"mOver('"+ id +"', '" + pic + "');\" "            +
        "onmouseout=\"mOut('"+ id +"', '" + origimg + "');\" "          +
        "onclick=\"mClick(id);\""                                       +
        ">" + text + "<br><img class='toolImg' src='img/tools/" + tool + "'></div>";
    }
    

    Which worked just fine in all browsers, including IE.