admin管理员组文章数量:1431426
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().
本文标签:
版权声明:本文标题:How to add an element in a HTML document using pure javascript at particular position of the document - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745567247a2663835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论