0

The default value of one on my radio boxes on my page is set to checked. I noticed that when I change the selection and reload the page, the check goes back to the default value. I was wondering whether there is a way in JavaScript to programatically set the value checked property of my radio elements.

This is my html:

<body>
<TD><INPUT TYPE="radio" NAME="Performer" VALUE="Aitken"   >Aitken</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Coltrane"  CHECKED>Coltrane</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Julliard" >Julliard</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Kronos"  >Kronos</TD>
  <TD><INPUT TYPE="radio" NAME="Performer" VALUE="Waits"  >Waits</TD>
<input type="submit" id="submit_button"name="sunmit">
</body>
7
  • So you're saying every time you reload the page, Coltrane is checked regardless of what you change it to? Commented Aug 20, 2012 at 20:15
  • yes...Coltrane is always selected regardless of what I changed it to Commented Aug 20, 2012 at 20:16
  • 1
    Are you somehow storing the change say via AJAX? If not, why would you expect the change to stick? Commented Aug 20, 2012 at 20:17
  • Actually I am not storing it using AJAX or any other mechanism. I figured that was the default behavior and was wondering whether it is possible to do that using javascript Commented Aug 20, 2012 at 20:18
  • To answer your question yes, you can store the value with JavaScript alone or in conjunction with a server side language. Commented Aug 20, 2012 at 20:19

3 Answers 3

1

Html, by nature, is stateless. Changing the value of something on a page does absolutely nothing, unless you actually do something with it.

In order to persist this value, you have to send it back to your server (either through AJAX, or a traditional form submit) and instead of having a hardcoded value, use a dynamic language of your choice.

You can also use local storage to have a javascript implementation that is specific to the user/machine.

Sign up to request clarification or add additional context in comments.

1 Comment

Or cookies, or window.name, or fragment identifier... No need to send it back to the server.
0

You can set a cookie each time someone checks/unchecks that contains all checked elements, then re-check them based on the cookie data on page load.

But I wouldn’t recommend it unless you know what you are doing as you will be jeopardizing usability when you do something "unexpected" with forms.

Comments

0

Once you know which button should be checked, using a cookie value or localStorage as other answers suggest, you can indeed check a button using javascript. Here is a jQuery snippet:

var valueToCheck = "Kronos";  // assume you have a known value like this one
$('input[name="Performer"][value="'+ valueToCheck +'"]').prop('checked', true);

If you do not have jQuery available you can use native code. Each input element has a value attribute and radio buttons also have a checked attribute:

var buttons = document.getElementsByTagName('input'), button;
for (var i = 0; i < buttons.length; i += 1) {
    button = buttons[i];
    if (button.name === "Performer" && button.value === valueToCheck) {
        button.checked = true;
        break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.