W
W
Worddoc2016-03-26 21:17:57
JavaScript
Worddoc, 2016-03-26 21:17:57

Did I create a cookie incorrectly?

Hello. I want to create cookies, the code seems to be correct, but it doesn't work, what's the matter?

<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();
}
document.getElementById("done").onclick = init();
</script>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Vitrenko, 2016-03-26
@Worddoc

document.getElementById("done").onclick = init;
If you put brackets after a variable that refers to a function, then you call this function, which means you pass the result of its execution. You don't need to put parentheses to pass the function itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question