Skip to content

Instantly share code, notes, and snippets.

@DonerKebab
Created November 25, 2019 15:58
Show Gist options
  • Save DonerKebab/932fe10211749c14937cf8a4e2ed7ec4 to your computer and use it in GitHub Desktop.
Save DonerKebab/932fe10211749c14937cf8a4e2ed7ec4 to your computer and use it in GitHub Desktop.
get selected value in radio button
<!DOCTYPE html>
<html>
<body>
<p>Please select your gender:</p>
<input type="radio" name="gender" id="r1" value="male"> Male<br>
<input type="radio" name="gender" id="r2" value="female"> Female<br>
<input type="radio" name="gender" id="r3" value="other"> Other<br>
<button onclick="selectValue()">Save</button>
<script>
function selectValue(){
if (document.getElementById('r1').checked) {
console.log(document.getElementById('r1').value);
} else if (document.getElementById('r2').checked) {
console.log(document.getElementById('r2').value);
} else if (document.getElementById('r3').checked) {
console.log(document.getElementById('r3').value);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment