i have a form on which i need to store the state of all radio buttons if a user submit the form with errors (which in case the page would refresh).
I want to achieve something quite similar to this:
$(function(){
$('.example input[type="radio"]:checked').each(function(){
$(this).attr("checked",true);
});
Any help would much be appreciated!
i have a form on which i need to store the state of all radio buttons if a user submit the form with errors (which in case the page would refresh).
I want to achieve something quite similar to this:
$(function(){
$('.example input[type="radio"]:checked').each(function(){
$(this).attr("checked",true);
});
Any help would much be appreciated!
Share Improve this question asked Sep 23, 2013 at 13:00 WasiimWasiim 1464 silver badges17 bronze badges 4-
You'll need to use cookies or HTML 5 local storage, or have the server generate new HTML based on the
POST
data. Lots of options, pretty mon problem. What's your server-side technology? – Yuck Commented Sep 23, 2013 at 13:02 - using any kind of persistent data client side as cookie or localStorage – A. Wolff Commented Sep 23, 2013 at 13:02
- Look here, I think this old post can help you stackoverflow./questions/16206322/… – BENARD Patrick Commented Sep 23, 2013 at 13:02
- before submit you can perform validations – Jagz S Commented Sep 23, 2013 at 13:03
3 Answers
Reset to default 5You could store it's state within local storage and restore it after each page load.
Little example on storing the value:
$('#myCheckbox').click(function () {
localStorage['my.checkbox'] = this.checked;
});
Restore it:
$('#myCheckbox').prop('checked', localStorage['my.checkbox'] == 'true');
Maybe you store the information in the session token or in a session cookie. If you use html5 ponents, you might try to use html local storage.
you just need to update the code
$(function(){
$('.example input[type="radio"]').each(function(){
$(this).attr("checked",$(this).checked());
});
It will save the property of radio button as user has selected for the radio button.