The javascript element creates the implementation method dynamically

  • 2020-06-12 08:25:19
  • OfStack

This article gives an example of how javascript elements are dynamically created. Share to everybody for everybody reference. The specific analysis is as follows:

document.write can only be created dynamically during page loading

You can call the createElement method of document to create an DOM object with the specified label, and then call the appendChild method of the element
The newly created element is added to the corresponding element

Examples are as follows:

<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Dom Dynamically creating elements </title>
 <script type="text/javascript">
 function CreateButton() {
  var div = document.getElementById("divMain");
  var myButton = document.createElement("input");
  myButton.type = "button";
  myButton.value = " I added it dynamically ";
  //"btn";  Pay attention to : If set id Avoid repetition 
  div.appendChild(myButton); // Added to the div on 
 <div id="divMain"></div>
 <input type="button" value=" Add elements " onclick="CreateButton()" />

I hope this article has been helpful for your javascript programming.

Related articles: