Skip to content
Advertisement

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!

Advertisement

Answer

Use the reset function, which is available on the form element.

var form = document.getElementById("myForm");
form.reset();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement