Storing the clicked value in radio buttonswith cookies

<>

This question is an exact duplicate of:

Answers


You need to set AND get the cookie.

FIDDLE

function setCookie(name, value, days) { // from http://www.quirksmode.org/js/cookies.html
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

window.onload = function () {
    var gender = getCookie('samplecookie');
    if (gender) {
        if (gender=="male") document.getElementById('male').click(); // or .checked=true; 
        else if (gender=="female") document.getElementById('female').click();
    }
    document.getElementById('buttons').onclick = function () {
        var m = document.getElementById('male');
        var f = document.getElementById('female');
        if (m.checked) {
            setCookie('samplecookie', m.value, 30);
        } else if (f.checked) {
            setCookie('samplecookie', f.value, 30);
        }
    }

}

Need Your Help

Sending input to a screen window from vim

vim clojure gnu-screen read-eval-print-loop

I have a vim function set up where I can highlight a line of text and execute in clojure. Here's the function:

xhr.setRequestHeader('X-CSRF-Token',token ) works in dev but not in production

ruby-on-rails ajax ruby-on-rails-3 jquery

In my Rails app i have an action that make ajax request. When i make this ajax request, Setting CSRF token with xhr.setRequestHeader works fine in development but in production (heroku) doesn't. ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.