jquery to dynamically create cookies using the jquery cookie plugin
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
jquery to dynamically create cookies using the jquery cookie plugin
hey guys,
I am learning jquery and I think for the most part I have the basics down. One of the things that I am attempting to do is to create cookies using the jquery cookie plugin. The idea is that I want to replace as much of the javascript in my code as possible with jquery to see the difficulties in doing it one way or the other.
As easy as it seems to be able to create a cookie using the plugin I have not been able to do some successfully and the only thing that I can think of is the way that I am submitting my parameters is not correct. So with that, here is is my code:
Code:
//loop over all input file of type text with class prompt
$('input:text, .prompt').each(function(){
//if the field has a value create a cookie then display the cookie in an alert
if($(this).val() != ''){
$name = $(this).attr('id');
$value = $(this).val();
$.cookie($name, $value;
$alert($.cookie($name));
};
});
Writing it out using javascript works perfectly fine.
Code:
$('input:text, .prompt').each(function(){
if($(this).val() != ''){
createCookie($(this).attr('id'),$(this).val());
};
});
function createCookie(cookieName,cookieValue){
var expDate = new Date(); // create a new date object
expDate.setMonth(expDate.getMonth()+1); //set expiration date to 1 month from today
document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expDate.toGMTString(); //create cookie
}
Any idea of what I could be doing wrong that which makes it so that I am not able to create a cookie using the cookie plugin for jquery?
Also, what is it that makes you think it doesn't work? Don't rely on that alert statement working right off the bat. You will often fall into a 'race condition' (actually not a real race condition, but close enough) where the time between setting a variable and getting that variable is too quick to expect it to work.
Try setting the cookie in jQuery in one function, and then is a separate function try and get it.
I've also never seen the '$' syntax used on your own variables.
Try this (not tested)
Code:
$('input:text, .prompt').each(function(){
//if the field has a value create a cookie then display the cookie in an alert
if($(this).val() != ''){
var name = $(this).attr('id');
var value = $(this).val();
$.cookie(name, value);
alert($.cookie(name));
};
});
Yes that was a copy paste issue sorry about that. Yes I do have the cookie plugin installed. Thought it would be nice to know if I installed it right. I simply stuck in the same place where my jquery.js file is and then included it in my html header tags.
Yes I did use the "$" for my variables because that is how I thought jquery used variables. Is that not the case?
I also wanted to have a create cookie method but could not figure out how to create another function using jquery code. In javascript I can simply call function someFunction() but in jquery I simply can't figure out how to do that same thing and then call it from within another function.
I will give your code a shot in a few minutes and let you know the results, thanks for the quick response.
I just gave it a shot but it didn't seem to work. I don't see it throwing any errors in the firebug console as well. I will take another look at the documentation again, maybe I am not loading the cookie.js file correctly or something.
Yes I did use the "$" for my variables because that is how I thought jquery used variables. Is that not the case?
Short answer: I don't know, I've never tried. I've only ever used the '$' notation to grab whole DOM elements as-in $('#blah') instead of document.getElementById('blah');
Quote:
I also wanted to have a create cookie method but could not figure out how to create another function using jquery code. In javascript I can simply call function someFunction() but in jquery I simply can't figure out how to do that same thing and then call it from within another function.
Assuming you've included the files in this order:
1). jquery.min.js (or whatever it's called)
2). cookie.min.js (or whatever it's called)
3). my-custom-js-file.js (or whatever the name of your file is you're working on)
..then you can create function just as you've always done - the difference is now you have the power of jQuery to mix and match as you please.
Code:
function getMyID(someID) {
return document.getElementById(someID);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.