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

How to dynamically add a checkbox using javascript_javascript skills

PHP中文网
PHP中文网Original
2016-05-16 15:24:052262browse

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");


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>

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 (www.php.cn)!





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