Search code examples
javascriptfirefoxgoogle-chromeparent

Javascript getElement field not working outside of google chrome


I wrote some code that works well on chrome. However, when running it on firefox it doesn't work. Firebug reports that the parentElement field in the getClass function, is not defined. It seems that maybe firefox wants another way to access the field like getParentElement or something. Does anyone have any other ideas that I could try?

function drawTile() {

    var newdiv = document.createElement('div');
    var newDivClass = document.createElement('div');
    newDivClass.setAttribute("class", "menu");
    var newul = document.createElement('ul');

    var tab1 = document.createElement('li');
    tab1.innerHTML = '<a href="#" onclick="getClass(this, 1);"> Tile </a>';

    newul.appendChild(tab1);
    newDivClass.appendChild(newul);
    newdiv.appendChild(newDivClass);

    var contentdiv2 = document.createElement('div');
    contentdiv2.setAttribute("id", "tabcontent2");
    contentdiv2.innerHTML = 'Data Manipulation Options';

    newdiv.appendChild(contentdiv2);
    rootTile.parent.appendChild(newdiv);
    //getClass(tab2.firstChild, 1); //Sets the tile to an initial state
}

function getClass(tile, selected) {
    var class = tile.parentElement.parentElement;
}

Solution

  • Use parentNode instead! parentElement isn't part of any standard, it's a MS invention that WebKit copied (and so it works in Chrome)