Skip to content

Commit b4fb919

Browse files
committed
Handling Radio Input Elements
1 parent 0f0cee0 commit b4fb919

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

DOM/checkbox-elements.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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 Checkbox Input Elements</title>
7+
<title>Handling Radio Input Elements</title>
88
<style>
99
h1 {
1010
color: white;

DOM/radio-elements.html

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,62 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Handling Radio Input Elements</title>
8+
<style>
9+
h1 {
10+
color: white;
11+
padding: 15px 10px;
12+
}
13+
</style>
814
</head>
915
<body>
10-
<div id="output"></div>
16+
<div id="output">
17+
<h1>Handling Radio Input Elements</h1>
18+
<form name="formDemo">
19+
<div id="radioArea">
20+
<label>
21+
<input type="radio" name="color" value="red" />
22+
Red
23+
</label>
24+
<label>
25+
<input type="radio" name="color" value="green" />
26+
Green
27+
</label>
28+
<label>
29+
<input type="radio" name="color" value="blue" checked />
30+
Blue
31+
</label>
32+
</div>
33+
</form>
34+
</div>
1135
<script>
1236
const output = document.getElementById("output");
37+
38+
const form = document.forms.formDemo;
39+
const radios = [...form.elements.color];
40+
console.log(radios);
41+
42+
radios.forEach((radio) =>
43+
console.log(`${radio.value} - ${radio.checked}`)
44+
);
45+
46+
//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);
60+
});
61+
62+
radios[2].select();
63+
radios[1].checked = true;
1364
</script>
1465
</body>
1566
</html>

0 commit comments

Comments
 (0)