<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3># 좋아하는 애완동물을 선택해주세요</h3>
<label><input type="radio" name="pet" value="강아지">
<span>강아지</span></label>
<label><input type="radio" name="pet" value="고양이">
<span>고양이</span></label>
<label><input type="radio" name="pet" value="햄스터">
<span>햄스터</span></label>
<label><input type="radio" name="pet" value="기타">
<span>기타</span></label>
<hr>
<h3 id="output"></h3>
<script>
document.addEventListener('DOMContentLoaded', () => {
const output = document.querySelector('#output');
const radios = document.querySelectorAll('[name=pet]');
radios.forEach((radio) => {
radio.addEventListener('change', (event) => {
const current = event.currentTarget;
if (current.checked) {
output.textContent = `좋아하는 애완동물은 ${current.value} 이시군요!`;
};
});
});
});
</script>
</body>
</html>