jquery - JavaScript font-size cookie -
jquery - JavaScript font-size cookie -
i'm kind of new javascript. i've created function changes font size , save size in cookie, user won't have alter every time. haven't got cookie part work.
i'm using next libraries: jquery javascript library v1.11.1 , https://developer.mozilla.org/en-us/docs/web/api/document.cookie
function change_size(element, size) { var new_size; var current = parseint(doccookies.getitem("fontsize")); if (current !== "") { current = parseint(element.css('font-size')); } if (size === 'smaller') { if (current > 12) { new_size = current - 2; } } else if (size === 'bigger') { if (current < 22) { new_size = current + 2; } } element.css('font-size', new_size + 'px'); doccookies.setitem("fontsize", num.tostring(new_size), infinity); } $('#smaller').click(function() { change_size($('p'), 'smaller'); }); $('#bigger').click(function() { change_size($('p'), 'bigger'); });
change line
doccookies.setitem("fontsize", num.tostring(new_size), infinity);
to
doccookies.setitem("fontsize", new_size, infinity);
you don't have num
variable.
also don't forget set new font size on page load:
var fontsize = doccookies.getitem("fontsize"); if (fontsize) { $('p').css('font-size', fontsize + 'px'); }
demo: http://jsfiddle.net/q56wkorw/ javascript jquery cookies
Comments
Post a Comment