Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'

Tags:

javascript

dom

I want to append an existing div to have a particular design along with an error message extracted out of an array.

This was my attempt:

    if (data.errors.firstName) {
        document.getElementById("firstName").classList.add("has-error");
        document.getElementById("firstName-group").appendChild('<div class="help-block"> </div>');
    }

But it resulted in an error:

TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.

Which after researching means that is a string which I can add in as it is not a valid dom node.

How can I do this properly?

like image 283
kaweki Avatar asked Oct 17 '25 11:10

kaweki


1 Answers

Your function is returning a string rather than the div node. appendChild can only append a node

var d= document.createElement("div");
d.classList.add("help-block");
document.getElementById("firstName-    group").appendChild(d);
like image 143
Osama Avatar answered Oct 19 '25 02:10

Osama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!