Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 808
Kizer Yakuza Avatar asked Mar 11 '13 16:03

Kizer Yakuza


People also ask

How do I clear the input field cache?

That solution helped me: if you are using google Chrome, try this: Put the mouse pointer on the entry that you want to delete, press Delete . If the entry is not removed, press Shift + Delete .

How do you clear form values?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

How do you prevent a form from clearing fields on submit?

You can use preventDefault method of the event object. Show activity on this post. Alternatively, you could use event.


2 Answers

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

var form = document.getElementById("myForm"); form.reset(); 
like image 65
thomaux Avatar answered Oct 14 '22 02:10

thomaux


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

var1.value = ''; var2.value = ''; 
like image 20
Rob M. Avatar answered Oct 14 '22 02:10

Rob M.