Sujata Hulsurkar
Sujata Hulsurkar

Reputation: 115

Unable to redirect after setting cookies in jQuery?

I have form which the users can fill to set their preferences. After they click a button I set cookies and then redirect. This is my code:

function allCookie() {
  var slvals = [];
  $('input:checkbox[name=checks]:checked').each(function() {
    slvals.push($(this).val());
  });
  document.cookie = document.getElementById('user').value + '=' + slvals + '; expires=Fri, 31-Dec-2030 23:59:59 GMT; path=/; domain=.website.com';
  document.cookie = '8MUC=' + document.getElementById('user').value + '; expires=Fri, 31-Dec-2030 23:59:59 GMT; path=/; domain=.website.com';
  window.location.href='http://www.website.com/';
}

Redirect occurs only if I have not filled out any of the form elements.

Upvotes: 0

Views: 107

Answers (1)

Clay Sills
Clay Sills

Reputation: 235

Try concatenating your checkbox values into a single string instead of putting them into an array. Then you can add that string to the final cookie value.

var slvals = '';
$('input:checkbox[name=checks]:checked').each(function() {
  slvals = slvals + this.val();
});

Upvotes: 1

Related Questions