Edwin Edwin - 1 year ago 51
HTML Question

Storing value of radio button in cookie not working in other browsers (Firefox), but works in Chrome

So, I have a submit button that saves the value of the radio button in a cookie. This works in chrome, but not in firefox...

<form name="radios" id="radios">
<input type="radio" id = "addition" name="browser" value="Addition" checked="checked">Addition<br>
<input type="radio" id = "subtraction" name="browser" value="Subtraction">Subtraction<br>
<input type="radio" id = "multiplication" name="browser" value="Multiplication">Multiplication<br>
<input type="radio" id = "division" name="browser" value="Division">Division<br>

I'm saving the cookie like this:

operation= escape(document.radios.browser.value) + ";";

Now, if I put document.radios.browser.value in the chrome console, it gives me the selected value for the radios.

However, if I do the same thing for firefox, it gives me "undefined."

How do I make this work for firefox? Is something wrong with my code?

Answer Source


operation = document.querySelector("form[name=radios] input[name=browser]:checked").value;

When you access document.radios.browser, it returns a NodeList containing all the inputs with that name, not just the selected one. Chrome apparently has an extension that allows you to access the value of this list, and it returns the value of the selected radio button. But this isn't standard Javascript, and doesn't work in other browsers.

Using the :checked modifier gets you the selected button, then you can access its value.