[javascript] Clear input fields on form submit

I know this is continuously asked anew, and I've checked out different answers and tried different solutions but to no avail. In some cases it can be really be a case by case thing depending on how the code has been redacted.

I'd like to simply have the two input fields in my page to clear when I click submit (&& enter - unless its a separate event, that's for another thread), considering I always press enter as opposed to clicking - this is a site just meant for my use.

I have of course a input tag, type submit, and its working fine. But I also already have a specific onSubmit function :

function onSubmitForm(e) {
    e.preventDefault();
    var var1 = document.getElementById("name");
    var var2 = document.getElementById("review");
    arrayOne.push( {name:var1.value,review:var2.value} );
    localStorage.filmDatabase = JSON.stringify(arrayOne);
    document.getElementById("eyedee").innerHTML += '<p>' + '<span class=name>' + var1.value + '</span>' + '<span class=review>' + var2.value + '</span>';
}

I'm guessing its just about putting one line of (right) code, most probably at the end of the block but I just couldn't figure it out. Thanks a lot for the help!

This question is related to javascript forms input

The answer is


You can clear out their values by just setting value to an empty string:

var1.value = '';
var2.value = '';

By this way, you hold a form by his ID and throw all his content. This technic is fastiest.

document.forms["id_form"].reset();

You can access the form over the event:

e.target.reset();

Still using empty strings you can use:

document.getElementById("name").value = '';
document.getElementById("review").value = '';

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to input

Angular 4 - get input value React - clearing an input value after form submit Min and max value of input in angular4 application Disable Button in Angular 2 Angular2 - Input Field To Accept Only Numbers How to validate white spaces/empty spaces? [Angular 2] Can't bind to 'ngModel' since it isn't a known property of 'input' Mask for an Input to allow phone numbers? File upload from <input type="file"> Why does the html input with type "number" allow the letter 'e' to be entered in the field?