44 < meta charset ="UTF-8 " />
55 < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
66 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7- < title > Handling Radio Input Elements</ title >
7+ < title > Handling Checkbox Input Elements</ title >
88 < style >
99 h1 {
1010 color : white;
1414 </ head >
1515 < body >
1616 < div id ="output ">
17- < h1 > Handling Radio Input Elements</ h1 >
17+ < h1 > Handling Checkbox Input Elements</ h1 >
1818 < form name ="formDemo ">
19- < div id ="radioArea ">
19+ < div id ="checkArea ">
2020 < label >
21- < input type ="radio " name ="color " value ="red " />
21+ < input type ="checkbox " name ="color " value ="red " />
2222 Red
2323 </ label >
2424 < label >
25- < input type ="radio " name ="color " value ="green " />
25+ < input type ="checkbox " name ="color " value ="green " />
2626 Green
2727 </ label >
2828 < label >
29- < input type ="radio " name ="color " value ="blue " checked />
29+ < input type ="checkbox " name ="color " value ="blue " checked />
3030 Blue
3131 </ label >
3232 </ div >
@@ -36,31 +36,18 @@ <h1>Handling Radio Input Elements</h1>
3636 const output = document . getElementById ( "output" ) ;
3737
3838 const form = document . forms . formDemo ;
39- const radios = [ ...form . elements . color ] ;
40- console . log ( radios ) ;
39+ const checks = [ ...form . elements . color ] ;
40+ console . log ( checks ) ;
4141
42- radios . forEach ( ( radio ) =>
43- console . log ( `${ radio . value } - ${ radio . checked } ` )
44- ) ;
42+ // checks[1].checked = true;
4543
4644 //Event
47- const radioArea = document . getElementById ( "radioArea" ) ;
48-
49- radioArea . addEventListener ( "change" , ( ) => {
50- // const checked = radios.find((item) => item.checked);
51- // console.log(checked.value);
52- // if (checked.value === "blue") {
53- // output.style.background = "blue";
54- // } else if (checked.value === "green") {
55- // output.style.background = "green";
56- // } else if (checked.value === "red") {
57- // output.style.background = "red";
58- // }
59- console . log ( form . elements . color . value ) ;
45+ const checkArea = document . getElementById ( "checkArea" ) ;
46+ checkArea . addEventListener ( "change" , ( ) => {
47+ checks . find ( ( radio ) =>
48+ console . log ( `${ radio . value } - ${ radio . checked } ` )
49+ ) ;
6050 } ) ;
61-
62- radios [ 2 ] . select ( ) ;
63- radios [ 1 ] . checked = true ;
6451 </ script >
6552 </ body >
6653</ html >
0 commit comments