index.html
<!DOCTYPE html>
<html>
<body>
<div id="main_div">
</div>
</body>
<script>
var values = ["aaa", "bbb"];
create_checkbox(values);
function create_checkbox(values) {
var main_div = document.getElementById('main_div');
for (var i = 0; i < values.length; i++) {
var checkbox = document.createElement('input');
var label = document.createElement('label');
var br = document.createElement('br');
var id = "checkbox" + i;
var name = "group1";
var value = values[i];
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("value", value);
checkbox.setAttribute("id", id);
checkbox.setAttribute("name", name);
label.setAttribute("for", id);
label.innerHTML = value;
if(value == "aaa"){
checkbox.checked = true;
}
checkbox.onclick = function() {
alert(this.value);
}
main_div.appendChild(br);
main_div.appendChild(checkbox);
main_div.appendChild(label);
}
}
</script>
</html>
|