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
appendChild() The appendChild() method of the Node interface adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.
append() is a convenient alternative to appendChild() . Like appendChild() , append() appends DOM nodes to an element, but unlike appendChild() , append() accepts multiple arguments of any types. The arguments that are not nodes are converted into strings that in turn are converted into text nodes.
removeChild() The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node.
append() can append several nodes and strings, whereas Node. appendChild() can only append one node.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With