Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript appendChild doesn't work

Tags:

javascript

dom

i'm having a bug on firefox 3.6 using this function

    function GetRefreshedResults(response)
    {   
        var splitted = response.value.split("|");
        var panel = document.getElementById('my-glider');
        var anchors = panel.getElementsByTagName('a');
        for (var i=0; i<anchors.length; i++)
        {
          anchors[i].innerHTML=splitted[i];
        }           
    }

which ads in DOM anchors like "< a xmlns="http://www.w3.org/1999/xhtml">

I'm now trying to use this instead:

     function GetRefreshedResults(response)
    {   
        var splitted = response.value.split("|");
        var panel = document.getElementById('my-glider');
        var anchors = panel.getElementsByTagName('a');
        for (var i=0; i<anchors.length; i++)
        {
            anchors[i].empty();
            anchors[i].appendChild(splitted[i]);
          //  anchors[i].innerHTML=splitted[i];
        }           
    }

but i get the following error in appendChild :

        Uncaught Error: NOT_FOUND_ERR: DOM Exception 8

i don't understand why it's not working. can anyone help me ? thanks

EDIT: Example:

splitted[0] contains :

   "<div class="var">Visits</div><div class="percent-zero">0%</div><div class="val">0<div class="val-alt">Unique Visits: 0</div></div>"

i want to update 8 anchors with new content, contained in splitted[0], splitted[1]...splitted[7]

like image 305
Dan Dinu Avatar asked Aug 12 '11 14:08

Dan Dinu


2 Answers

splitted[i] is the problem. appendChild appends a DOM-element to an existing DOM-element, but it looks like you ar trying to append a string value. If you want to use appendChild, either create a container element and use innerHTML for that to insert the string, or just use innerHTML. It is not a bug that you can't append a string as DOM-element, I'd say. See also the MDN-page on appendChild.

like image 188
KooiInc Avatar answered Oct 11 '22 13:10

KooiInc


response.value.split("|"); Indicates to me that you are passing response as a string. appendChild only works with elements. You can't append a child to a flat string.

like image 24
Joseph Marikle Avatar answered Oct 11 '22 14:10

Joseph Marikle