フォームの値の取得 - Javascript
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>sample</title>
<script src="script.js"></script>
</head>
<body>
<form action="" name="myform">
<p>性別
<label><input type="radio" name="gender" id="" value="男" checked>男</label>
<label><input type="radio" name="gender" id="" value="女">女</label>
</p>
<p><input id="btn" type="button" value="button"></p>
</form>
</body>
</html>
script.js
window.onload = function(){
let btn = document.getElementById('btn');
btn.addEventListener('click', function(){
let gender = document.getElementsByName('gender');
console.log(gender); // NodeList(2) [input, input]
for(let i = 0; i < gender.length; i++){
if(gender[i].checked){
const radioValue = gender[i].value;
console.log(radioValue);
};
};
});
};