I cannot figure out how to pass an HTML input to a JS variable.
<form id="form" target="_self">
<input type="text" id="task" placeholder="What's on the agenda?" onsubmit="getForm()">
</form>
My HTML form with the function being called as follows:
function getForm() {
var form = document.getElementById("task").value;
console.log(form);
}
However, when I press enter after typing into the input text, it just refreshes the page and changes the URL from index.html to index.html?task=foo and doesn't log anything in the console.
onsubmitgoes on the form tag, not the input.event.preventDefault()formtag submits your form to a server which results in refreshing the page. Also, because your form method isGET, your input value is appended to the url. What are you trying to do?