Alberto Rossi
Alberto Rossi

Reputation: 1800

Read cookie text to input value

This is all the jQuery code I have in my page. Thanks to this, I can click on a label, change the body background and store it into a cookie. In this way, everytime I open the page I see the background I saved.

var setBackground = function(bgImg) {
    $.cookie('bgImg', bgImg, { expires: 720 });
    $('body').css('background-image', 'url(http://escaperope.altervista.org/testsito/' + bgImg+ '.jpg)');
}

$(function() { 
    $('#bg1').click(function() {
        setBackground('xerneas');
    });
    $('#bg2').click(function() {
        setBackground('yveltal');
    });
    setBackground($.cookie('bgImg'));

   $('#salvauser').click(function(name) {
        $.cookie('escrop_user', $('#nome').val(), { expires: 720 });
   });
});

By the way I am having problems with the last function. Where you see $('#salvauser') I save on a cookie the value of an input and it works. When I close the page and I open it again, I must load inside the input salvauser the content of that cookie.

I am pretty new with jQuery. How could I do it?

I tried with this code but it doesn't work.

  $('#salvauser').val() = $.cookie('escrop_user');

Upvotes: 0

Views: 94

Answers (1)

Venkata Krishna
Venkata Krishna

Reputation: 15112

The correct format is $(selector).val(text)

$('#salvauser').val($.cookie('escrop_user'))

Upvotes: 2

Related Questions