0

I am passing the FORM values to a cookie via jQuery Cookie. Am I doing something wrong I can not get the cookie values to save

<script src="/assets/jquery.cookie.js"></script>
<script type="text/javascript">
;(function ($) {
    $('input[name="FirstName"]').change(function(){
        $.cookie("FirstName", $('input[name="FirstName"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="LastName"]').change(function(){
        $.cookie("LastName", $('input[name="LastName"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="Email"]').change(function(){
        $.cookie("Email", $('input[name="Email"]').val();, {path: "/", domain: "domain.com"});
    });
    $('input[name="Category"]').change(function(){
        $.cookie("Category", $('select[name="Category"]').val();, {path: "/", domain: "domain.com"});
    });
})(jQuery);
</script>

HTML

<form method='post' action='send.php' name='demo'>
 <input type='text' name='FirstName' value='' id='FirstName' />
 <input type='text' name='LastName' value='' id='LastName' />
 <input type='text' name='Email value='' id='Email' />
 <select name='Category'>
  <option value='Blue'>Blue</option>
  <option value='Red'>Red</option>
  <option value='Green'>Green</option>
 </select>
</form>

1 Answer 1

1

There is a syntax error in your code, remove the ;:

$.cookie("FirstName", $('input[name="FirstName"]').val();, {path: "/", domain: "domain.com"});
                                                // ---- ^

Also note that ;(function ($) { ... })(jQuery) is a closure, it doesn't do what document ready handler does.

jQuery(function($) { // The first argument of the document ready handler refers to jQuery
    $('input, select').change(function(){
        $.cookie(this.name, this.value, {path: "/", domain: "domain.com"});
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks... also is $(window).load(function() needed ?
@acctman No. It doesn't help here. Using DOMReady is sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.