Why is AppendChild not working!


#1

Hello,

We are trying to create 2 DIVs, append the 2nd created DIV into 1st created DIV and append the whole thing into the static DIV. The Append works fine in case of Appending the 1st created DIV into the static DIV but not in case of appending the created DIV into created DIV. As you can see here:

https://www.anoox.com/paid/reasons_to_buy_test.php?type=yes&afm=yes

by clicking on: Show All

It produces JS Error message:

Uncaught TypeError: Cannot read property 'appendChild' of null

Even though the DIV is created and defined via:

newNode.innerHTML = res_list[indx];
newNode.className = ('flat_top w3-animate-left bott_container');  
newNode.id = idval;

What is the fix to this?

Thanks,


#2

Hi there WorldNews,

you need to swap the code on lines #775 and #777 around. :winky:

So instead of ...

    document.getElementById(idval).appendChild(num_node);

    document.getElementById('main_page').appendChild(newNode);

...it should be...

    document.getElementById('main_page').appendChild(newNode); 
	
    document.getElementById(idval).appendChild(num_node);

Also note that the "Web Console" points out that your code is
missing these two functions...

function display_div() {
    // your code here
 }

function close_div() {
    // your code here
 }

coothead


#3

Coothead,

Yes that lil change takes care off the problem. Thanks.
And function display_div() {
// your code here
}
are loaded via an external js file.

Cheers


#4

 
 
        No problem, you're very welcome. :winky:
 
 
        coothead