<checkbox 선택항목 Textarea에 넣기>
#Mission
1. checkbox에서 check된 선택항목 TextArea에 넣기
2. checkbox 이용해서 '전체선택' 만들기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
</style>
</head>
<body>
<form>
<label><input type="checkbox" onclick="checkAll()">전체 선택</label><br>
<label><input type="checkbox" name="sport" value="농구" onclick="slicked()">농구</label><br>
<label><input type="checkbox" name="sport" value="야구" onclick="slicked()">야구</label><br>
<label><input type="checkbox" name="sport" value="축구" onclick="slicked()">축구</label><br>
<label><input type="checkbox" name="sport" value="하키" onclick="slicked()">하키</label><br><br>
<textarea name="textbox" cols="60" rows="20" ></textarea>
</form>
<script>
//Textarea에 checked된 value 넣기
function slicked(){
var checkbox = document.getElementsByName('sport');
var checked = [];
for( var i=0; i<checkbox.length; i++){
if(checkbox[i].checked){
checked.push(checkbox[i].value);
}
}
document.querySelector("textarea").innerHTML = checked;
}
//전체 선택
function checkAll(){
var allbox = document.getElementsByName('sport');
var checkedall = [];
for(var i=0; i<allbox.length; i++){
allbox[i].checked = true;
checkedall.push(allbox[i].value);
}
document.querySelector("textarea").innerHTML = checkedall;
}
</script>
</body>
</html>
| cs |
(Ctrl+F1) Result>
댓글 없음:
댓글 쓰기