W
W
Worddoc2016-03-27 17:32:12
JavaScript
Worddoc, 2016-03-27 17:32:12

Why doesn't the cookie work?

Hello. I create cookies, but for some reason the input value is not written to the cookie value. What did I do wrong?

<form>
  <input type="text" id="first" placeholder="Введите имя">
  <input type="button" id="done" value="Готово!">
</form>

<script>
  var init = function() {
  var expDate = new Date();
  expDate.setMonth(expDate.getMonth() +1);
  var cookieVal = document.getElementById("first").value;
  document.cookie = "first" + "=" + cookieVal + ";path=/;expires=" + expDate.toGMTString();
  var valArray = document.cookie.split(";");
  return valArray[1];
}
  document.getElementById("done").onclick = init;
  document.getElementById("first").value = init();

</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Dvoryanov, 2016-03-28
@Raxen

Everything works fine. The First cookie is created with the contents of the input

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question