-
checkbox in javascriptFront-End/javascript 2018. 11. 4. 16:41
<input type="checkbox" id="myCheck1" name="myCheck" value="1" onclick="myFunction()">
<input type="checkbox" id="myCheck2" name="myCheck" value="2" onclick="myFunction()">
<input type="checkbox" id="myCheck3" name="myCheck" value="3" onclick="myFunction()">
<input type="checkbox" id="myCheck4" name="myCheck" value="4" onclick="myFunction()">
function myFunction() {
// 1. 이름으로 객체 가져오기
var checkBox = document.getElementsByName("myCheck");
// 2. 체크설정
checkBox[1].checked = true;
// 3. 체크된값 가져오기
for(var i=0,max=checkBox.length;i<max;i++) {
if(checkBox[i].checked)
console.log(checkBox[i].value);
}
}