How to display "selected radio button" after refresh?

17,944

Solution 1

You should conform to standards and give a value to every attribute. That means you need checked="checked"

<input name="radiobutton" type="radio" value="" id="all" checked="checked" />

And in case you were asking how to stick to what was selected previously: AFAIK, it is not possible with plain HTML. You need to bind a Javascript event that submits the selected radio button to your server and a dynamically generated page (by PHP, Python, whatever) that puts the 'checked="checked" to the radio button that was submitted as checked.

EDIT:

As I see it, submitting the currently selected radio button to the server and generating your page dynamically might be overkill.

Solution 2

I don't know if this is something you'd want. But a thing that comes up to mind is the HTML5 Web Storage functionality.With that feature you can store data on the computer of the user.

So whenever a user changes an input field you can create a javascript call that stores the value into the localstorage:

localStorage.setItem(“inputName”, “value”);

Then when you load the page, you see if any of these values are stored and then fill them in.

Share:
17,944
Always21
Author by

Always21

Updated on June 05, 2022

Comments

  • Always21
    Always21 almost 2 years

    After refresh the page, the user selected radio button is missing. How can I always display the user's "selected radio button" even I had refresh the page?

    <input name="radiobutton" type="radio" value="" id="all" checked />
    

    radio with checked is not suitable, because it will always display the default radio button.

    Below is my code:-

    <form name="myfrm" id="myfrm" action="" method="post">
        <input name="radiobutton" type="radio" value="" id="all" />
        <label>All Languages</label>
        <input name="radiobutton" type="radio" value="" id="english" />
        <label>English</label>
    </form>
    
  • Vivek
    Vivek about 13 years
    what?????? it will always show that checkbox as checked irrespective of it's checked by user or not...first read the question, then answer...
  • riha
    riha about 13 years
    Sorry, I misunderstood the question at first.
  • Always21
    Always21 about 13 years
    Actually, this would be my first time posting question on stackoverflow.com. Never expect I will get the response so fast.
  • Always21
    Always21 about 13 years
    Let's me figure out every suggestion and answer first. thanks.
  • Always21
    Always21 about 13 years
    Thanks for your suggestion and answer.
  • Always21
    Always21 about 13 years
    Thanks for your suggestion and answer.