Home >Web Front-end >JS Tutorial >Js dynamically add checkbox Checkbox instance method_javascript skills
First of all, using JS to dynamically generate a Checkbox can use statements similar to the following:
The following code, the program generates a checkbox and a text node, puts them into a li object, and then adds the li object to the ul object:
for(var i=0;i
// Add checkbox
var checkBox=document. createElement("input");
checkBox.setAttribute("type","checkbox");
checkBox.setAttribute("id",arr[0]);
checkBox.setAttribute("name" , arr[1]);
var li=document.createElement("li");
li.appendChild(checkBox);
li.appendChild(document.createTextNode(arr[1] ));
ul.appendChild(li);
In the above code, put the checkbox in li and ul, which can achieve a good arrangement effect. The CSS styles set by UL and li are as follows: