Home > Web Front-end > JS Tutorial > How to dynamically add a checkbox using javascript_javascript skills

How to dynamically add a checkbox using javascript_javascript skills

PHP中文网
Release: 2016-05-16 15:24:05
Original
2324 people have browsed it

The example in this article introduces how to dynamically add a checkbox using JavaScript:
In actual applications, it may be necessary to dynamically add a checkbox. Here is a brief introduction on how to achieve this effect.
It is very easy to simply create a checkbox, the code is as follows:


var oCheckbox=document.createElement("input");
oCheckbox.setAttribute("type","checkbox");
oCheckbox.setAttribute("id","mayi");
Copy after login


But this It just creates a checkbox object, but it often cannot meet actual needs, because in actual applications, there is usually explanatory text in front of or behind the checkbox. Here is an introduction to how to achieve this effect:
The method is to create a checkbox object, then create a text node, and then add it to p.

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8">
<title>添加checkbox复选框</title>
<script type="text/javascript"> 
var oCheckbox=document.createElement("input");
var myText=document.createTextNode("蚂蚁部落");
oCheckbox.setAttribute("type","checkbox");
oCheckbox.setAttribute("id","mayi");
window.onload=function(){
 var mydiv=document.getElementById("mydiv");
 mydiv.appendChild(oCheckbox);
 mydiv.appendChild(myText);
}
</script> 
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
Copy after login

The above is the method of dynamically adding a checkbox in javascript_javascript skills. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!





source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template