There are two methods for generating HTML elements in Javascript. The first is a more formal element creation method, and the second is to directly write html elements using the write() method in Javascript.
Method 1:
The code copy is as follows:
//createElement() creates input element into obj object
var obj = document.createElement('input');
//Select the previous element of the location to be generated
var before = document.getElementById('before');
//Set the obj element name and value
obj.name='name';
obj.value='value';
//setAttribute can be freely defined. Not only id and type, name and value can also be defined here.
obj.setAttribute('id','idname');
obj.setAttribute('type','typename');
//Add obj after the previous element
before.appendChild(obj);
Method 2:
The code copy is as follows:
document.write("<inpt name='name' value='value' id='idname' type='typename'>");
Here are two methods to generate <input> elements of html. The first one is more standardized, with more code, and the second one is less code.