I know I can add an element using javascript by:
document.createElement('input');
But I want to add it in a particular position of the document, like after the submit
button, or after the textfield
I know I can add an element using javascript by:
document.createElement('input');
But I want to add it in a particular position of the document, like after the submit
button, or after the textfield
-
2
The function
.addElement
does not exist in the DOM afaik. – Felix Kling Commented Jan 15, 2013 at 12:33 - 1 I remend to read w3/wiki/Creating_and_modifying_HTML. This might also help: w3/wiki/Traversing_the_DOM. – Felix Kling Commented Jan 15, 2013 at 12:35
- @FelixKling Sorry createElement – Govind Balaji Commented Jan 15, 2013 at 12:35
- or look at this dustindiaz./add-remove-elements-reprise – Rachel Gallen Commented Jan 15, 2013 at 12:39
2 Answers
Reset to default 3Try this out:
s
is the element you want to put the new element after.
var s = document.getElementsByTagName("input")[0];
var newNode = document.createElement("input");
s.parentNode.insertBefore(newNode, s.nextSibling);
This will place the new input
after the current input
.
Alternatively:
var f = document.getElementsByTagName("form")[0];
var newNode = document.createElement("input");
f.appendChild(newNode);
You can get a node with one of the GetElementsBy functions, and then append new DOM Elements with Node.appendChild().