Answer the question
In order to leave comments, you need to log in
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
Everything works fine. The First cookie is created with the contents of the input
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question