I'm generating some radio buttons dynamically. The end result looks like this:
<div>Question 1:</div>
<input type="radio" name="1" value="1">Yes
<input type="radio" name="1" value="0">No
<div>Question 2:</div>
<input type="radio" name="2" value="1">Yes
<input type="radio" name="2" value="0">No
<div>Question 3:</div>
<input type="radio" name="3" value="1">Yes
<input type="radio" name="3" value="0">No
I need to create an Array from the selected radio buttons that looks like this:
"someName": {
"1": "1",
"2": "0"
}
I tried the following but the created array is not what I want:
let names = ["1","2","3"]
let results = [];
document.querySelector("button").addEventListener("click", function(event){
results = names.map(function(el){
return document.querySelector("input[name='" + el + "']:checked").value;
});
console.log(results);
});
This will create an array like this:
[
"1",
"2",
"3"
]
How can I achieve the array I want?