0

I'm having a problem with getting a radio button to deselect.

I've got the following buttons:

<input type="radio" name="sex" value="Male">Male</input>
<input type="radio" name="sex" value="Female">Female</input>
<input type="radio" name="sex" value="Unknown">Unknown</input>

In the pagebeforeshow I've got:

$('input:radio[name=sex]').attr('checked',false);

The first time the page displays, no button is selected. I select a button and dismiss then page and when I redisplay the page the radio button is still selected,

What am I missing?

Some additional details:

In pagebeforeshow I use

$('input:radio[name=sex]:checked').val();

to print out the value of the checked radio button.

The first time the page displays the value is

undefined

and no radio buttons show as checked when the page display.

I select a radio button and leave the page. Before I exit the page, I use:

$('input:radio[name=sex]').attr('checked',false);

to deselect the button.

When I navigate back to the page, pagebeforeshow prints the value as

undefined

which I expect.

But when the page displays the audio button that was selected when I left the page is still selected.

How could the value print as undefined but the button show as checked when the page display?

user1126515
  • 1,133
  • 3
  • 17
  • 34
  • 1
    do you mean that its still the same on reload? – Rachel Gallen Jun 23 '16 at 00:59
  • Try `.prop('checked', false)` instead. See http://stackoverflow.com/questions/5874652/prop-vs-attr – Phil Jun 23 '16 at 01:02
  • the fiddle works fine https://jsfiddle.net/RachGal/8h2ds5mh/ note that you don't need the closing – Rachel Gallen Jun 23 '16 at 01:07
  • Sounds like you are calling that code before elements exist – charlietfl Jun 23 '16 at 01:14
  • 2
    Redisplaying a page doesn't normally select any of the radio buttons if they don't have a `checked` attribute. You must have some other Javascript code that's saving the state in a cookie or localStorage, and then checking the button. – Barmar Jun 23 '16 at 01:31
  • @Barmar Doesn't Firefox [autocomplete inputs](http://stackoverflow.com/questions/7377301/firefox-keeps-form-data-on-reload) when refreshing a page ? Then OP should use `autocomplete="off"` in its inputs. – tektiv Jun 23 '16 at 13:13
  • When you say that you redisplay the page, are you talking about using the Back/Forward button, or do you mean the Refresh command that reloads the page? Back and Forward will restore the state of inputs, but reloading doesn't. – Barmar Jun 23 '16 at 14:02

0 Answers0