Javascript Node Issue

Discussion in 'Coding Help' started by ziycon, Nov 3, 2010.

Tags:
  1. ziycon

    ziycon New Member

    I have this code that doesn't work in FF due to a bug with the firstchild bit:
    Code:
    var opts = obj.childNodes;
      
    var li = document.createElement('li');
    var txt = document.createTextNode(opts[0].firstChild.firstChild.nodeValue);
    And I want to replace it with the below, but I'm not sure how or what I pass to the function?
    Code:
    var opts = obj.childNodes;
      
    var li = document.createElement('li');
    var txt = document.createTextNode(opts[0].getFirstChild().getFirstChild().nodeValue);
    Code:
    function getFirstChild(elm){
        if ( !elm.childNodes.length ){
            return;
        }
        var children = elm.childNodes.length;
        for ( var i = 0; i <= children; ++i ){
            if ( elm.childNodes[i].nodeType == 1 ){
                return elm.childNodes[i];
            }
        }
        return;
    }

     

Share This Page