By wrapping an input element inside of a label element it will automatically associate the radio button input with the label element surrounding it.
access form & elements
โญ๏ธ document forms are members of document.forms
โญ๏ธ form elements are available in form.elements
const {log} = console;/* โญ๏ธ document forms are members of `document.forms` โญ๏ธ form elements are available in `form.elements`*/// โญ๏ธ get <form> by "name"let form =document.forms.form1; // <form name="form1">// = document.forms[0]// โญ๏ธ get <input> by "name"let input =form.elements.age; // <input name="age">log(input.value); // 17// โญ๏ธ being radio buttons, `form.elements['sex']` is a collectionlet sex =form.elements.sex; // RadioNodeList โญ๏ธ// โญ๏ธ value of checked radio button ('' if none checked โญ๏ธ)log(sex.value) // "female"// โญ๏ธ turn collection into arraylet values = [...sex].map(input =>input.value)log(values) // ["male","female"]
<form name="form1">
<!-- type="text" -->
<input name="name" value="Joy">
<input name="age" value="17">
<!-- โญ๏ธ radio buttons with same "name" -->
<input type="radio" name="sex" value="male">
<input type="radio" name="sex" value="female" checked>
</form>