How to add an element in a HTML document using pure javascript at particular position of the document - Stack Overflow

I know I can add an element using javascript by:document.createElement('input');But I want

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

Share Improve this question edited Feb 1, 2015 at 16:40 Deduplicator 45.8k7 gold badges72 silver badges123 bronze badges asked Jan 15, 2013 at 12:32 Govind BalajiGovind Balaji 6417 silver badges17 bronze badges 4
  • 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
Add a ment  | 

2 Answers 2

Reset to default 3

Try 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().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567247a4633469.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信