Skip to content

Instantly share code, notes, and snippets.

@burt202
Created August 16, 2021 14:02
Show Gist options
  • Save burt202/69bfd65e4d359b95fe5ce7be2d2bdeca to your computer and use it in GitHub Desktop.
Save burt202/69bfd65e4d359b95fe5ce7be2d2bdeca to your computer and use it in GitHub Desktop.
Getting radio values
<html>
<body>
<h1>Group 1</h1>
<div>
<input type="radio" id="group1-aaa" name="group1" value="aaa" />
<label for="aaa">AAA</label>
</div>
<div>
<input type="radio" id="group1-bbb" name="group1" value="bbb" />
<label for="bbb">BBB</label>
</div>
<div>
<input type="radio" id="group1-ccc" name="group1" value="ccc" />
<label for="ccc">CCC</label>
</div>
<hr />
<h1>Group 2</h1>
<div>
<input type="radio" id="group2-aaa" name="group2" value="aaa" />
<label for="aaa">AAA</label>
</div>
<div>
<input type="radio" id="group2-bbb" name="group2" value="bbb" />
<label for="bbb">BBB</label>
</div>
<div>
<input type="radio" id="group2-ccc" name="group2" value="ccc" />
<label for="ccc">CCC</label>
</div>
<script type="text/javascript">
const radioNodes = document.querySelectorAll('[type="radio"]')
function getRadiosWithValue(nodes, value) {
return Array.from(nodes).filter((r) => r.value === value)
}
console.log("filtered", getRadiosWithValue(radioNodes, "aaa"))
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment