mercredi 5 février 2020

How do I submit data from a text field in HTML to a JavaScript variable?

I am very new to web development but I am familiar with programming in general. I'm trying to learn by creating a simple task, a tracking application. However, 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.




Aucun commentaire:

Enregistrer un commentaire