How to reset (clear) form through JavaScript?

I have tried $("#client.frm").reset(); but it is not working.So how to reset form via jQuery?

2

13 Answers

form.reset() is a DOM element method (not one on the jQuery object), so you need:

$("#client.frm")[0].reset(); //faster version: $("#client")[0].reset(); 

Or without jQuery:

document.getElementById("client").reset(); 

Note: reset() function does not work if form contains any field with attribute:

name='reset' 
3

You can simply do:

$("#client.frm").trigger('reset')

1

Pure JS solution is as follows:

function clearForm(myFormElement) { var elements = myFormElement.elements; myFormElement.reset(); for(i=0; i<elements.length; i++) { field_type = elements[i].type.toLowerCase(); switch(field_type) { case "text": case "password": case "textarea": case "hidden": elements[i].value = ""; break; case "radio": case "checkbox": if (elements[i].checked) { elements[i].checked = false; } break; case "select-one": case "select-multi": elements[i].selectedIndex = -1; break; default: break; } } } 
5

Note, function form.reset() will not work if some input tag in the form have attribute name='reset'

1

The .reset() method does not clear the default values and checkbox field and there are many more issues.

In order to completely reset check the below link -

0

Clear the form as follows

document.forms[0].reset(); 

You can simply clear the form elements within the group. by using this forms[0].

Reset (Clear) Form throught Javascript & jQuery:

Example Javascript:

document.getElementById("client").reset(); 

Example jQuery:

You may try using trigger() Reference Link

$('#client.frm').trigger("reset"); 

Use JavaScript function reset():

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

Try this :

$('#resetBtn').on('click', function(e){ e.preventDefault(); $("#myform")[0].reset.click(); } 

You could use the following:

$('[element]').trigger('reset') 

Try this code. A complete solution for your answer.

 <!DOCTYPE html> <html> <head> <script src=""></script> <script> $(document).ready(function(){ $(":reset").css("background-color", "red"); }); </script> </head> <body> <form action=""> Name: <input type="text" name="user"><br> Password: <input type="password" name="password"><br> <button type="button">Useless Button</button> <input type="button" value="Another useless button"><br> <input type="reset" value="Reset"> <input type="submit" value="Submit"><br> </form> </body> </html> 
0

Use this simple trick to reset the form

_("form_id").reset(); 

You can clear the whole form using onclick function.Here is the code for it.

 <button type="reset" value="reset" type="reset" onclick="window.location.reload()">Reset</button> 

window.location.reload() function will refresh your page and all data will clear.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like