I have a php page on which I want a user can change the color of some elements, and next time he load the page his choices should be loaded by default.
I'm using jQuery click()
function to change the colors, and to save the changes on server side I suppose that the best way is using jquery/json, but I'm totally new about this.
Should I have a separate json file with color options stored? I found so many stories on web about jquery/json, but still need an example how to accomplish this task. Please help with a useful link or code example.
#div01 {
background:#008080;
color:#ffffff;
}
$('#btnBlue').click(function () {
$('#div01').css('background', '#0000ff');
$('#div01').css('color', '#ffffff');
});
$('#btnRed').click(function () {
$('#div01').css('background', '#ff0000');
$('#div01').css('color', '#0000ff');
});
If you are looking for a concrete example on how to do this, this might do the trick. First, use this JavaScript library: https://github.com/carhartl/jquery-cookie.
Then, this would be your script:
var backgroundColor = $.cookie('backgroundColor'), color = $.cookie('color');
$('#div01').css('background', backgroundColor);
$('#div01').css('color', color);
$('#btnBlue').click(function () {
$.cookie(backgroundColor, '#0000ff');
$.cookie(color, '#ffffff');
$('#div01').css('background', backgroundColor);
$('#div01').css('color', color);
});
$('#btnRed').click(function () {
$.cookie(backgroundColor, '#ff0000');
$.cookie(color, '#0000ff');
$('#div01').css('background', backgroundColor);
$('#div01').css('color', color);
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments