AL_1
AL_1

Reputation: 112

Trying to set cookie, but it says undefined. What do i do?

So here is my code

<script>
function ls() {
var text = document.getElementById("name").value;
document.cookie = "username=" + text + ";";
document.getElementById("name").innerHTML = text;

}
</script>
<p id="name"><p>
<input type="text" id = "textbox"> <br/>
<input type="Submit" value = " Submit " onClick = "ls()">

And when i run it and enter a value into the text box the press submit it creates the cookie but with a "undefined" as the value. Also the "name" text is set to "undefined"

Anyone know what im doing wrong?

Upvotes: 0

Views: 503

Answers (1)

Swimburger
Swimburger

Reputation: 7184

You confused your id's. Use textbox instead of name.

var text = document.getElementById("textbox").value;

Upvotes: 2

Related Questions