I am trying to access the value of a checked radio button in my form using JavaScript only, no jQuery. I have looked this up and seen many answers but when I copy the same code into my project it doesn't work for some reason. Here is my code.
<form>
<input type="radio" name="style" value="3" checked>
<input type="radio" name="style" value="5">
<input type="radio" name="style" value="10">
</form>
I tried to access the value using this JavaScript code:
var value = document.querySelector('input[name="style"]:checked').value;
console.log(value);
I would assume this would give me the string of 3 as the logged value. When I try this example I get the following error:
request.js:1 Uncaught TypeError: Cannot read property 'value' of null
at request.js:1
I also tried using a for loop of an array of the inputs named style and that didn't work either. It found all three inputs but didn't have values for them in the array. Can someone please explain what is wrong? And if this is obvious please be patient I am still learning I am just trying to get better.