Home >Web Front-end >JS Tutorial >Dynamically adding form elements input, button, etc. (appendChild) implemented in javascript_javascript skills

Dynamically adding form elements input, button, etc. (appendChild) implemented in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:07:371457browse

When writing a small system, you need to dynamically add form elements, so I wrote this tutorial according to my own implementation method!
I think you have seen similar effects on many websites!
1. Use document first. The createElement method creates an input element!

Copy code The code is as follows:

var newInput = document.createElement("input");

2. Set related attributes, such as name, type, etc.
Copy code The code is as follows:

newInput.type=mytype;
newInput.name="input1";

3. Use the appendChild method to append elements to the content of a tag !
Copy code The code is as follows:

TemO.appendChild(newInput);

Javascrip core code:
Copy code The code is as follows:




[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn