Is there a way to use document.querySelector("#") on an input element (and check if it is checked) that is saved in a string and then retrieved by innerHTML? I'm having trouble figuring out how to access it.
I am trying to loop through an array and log when a radio button is checked off. I have attempted using querySelectorAll("radio") with an array of querySelectors for the different ids of the radio buttons, but it doesn't seem to be working.
<body>
<div class="stemmegiving"> </div>
<script>
var stemmegivingEl= document.querySelector(".stemmegiving")
var alt=['<input type="radio" name="stem" class="kanpper" checked id=" Rødt" >',
'<input type="radio" name="stem" value=""class="kanpper"id="SP"> ',
'<input type="radio" name="stem" value=""class="kanpper"id="AP"> ']
for (var i = 0; i < alt.length; i++) {
stemmegivingEl.innerHTML+= alt[i]
}
var giStemmeEl = document.querySelector("#giStemme")
var alt2=[document.querySelector("#rødt"), document.querySelector("#SV"),document.querySelector("#AP")]
for (var i = 0; i < alt.length; i++) {
if (alt2[i].checked) {
console.log(alt2[i] + " is true");
console.log(typeof alt2[i]);
}
else {
console.log(i + "not true");
console.log(typeof alt2[i]);
}
}
</script>
</body>
I want to find a way to access it the same way as if you create an input element in the body and then querySelect it.