AppendChild() is not a function javascript

.net, html, javascript

Solution

Your `div` variable is a string, not a DOM element object:

var div = '<div>top div</div>';

Strings don't have an `appendChild` method. Instead of creating a raw HTML string, create the div as a DOM element and append a text node, then append the input element:

var div = document.createElement('div');
div.appendChild(document.createTextNode('top div'));

div.appendChild(element);

Problem

this is my javascript ``` <head runat="server"> <script type="text/javascript" language="javascript"> function createQuestionPanel() { var element = document.createElement("Input"); element.setAttribute("type", "button"); element.setAttribute("value", "button"); element.setAttribute("name", "button"); var div = '<div>top div</div>'; div[0].appendChild(element); } function formvalidate() { } </script> </head> <body onload="createQuestionPanel()"> </body> </html> ``` it is throwing error "AppendChild is not a function" . I tried to search for solution .. it was suggested that on the place of div.appendChild(element); this should be posted div[0].appendChild(element); it didnt change the error . Please suggest

Original source