Avatar of hankknight
hankknight
Flag for Canada asked on

jQuery: Reset Form

Using jQuery, how can I reset a form?  I can do this using traditional JavaScript but I do not know how to do it with jQuery.  Please test the example below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('#abc').val('');
 $('#xyz').val('555');

 $('p a#reset1').click(function() {
  document.getElementById('myform').reset();
 });
 $('p a#reset2').click(function() {
  $('#myform').reset();
 });

});

</script>
</head>
<body>

<form action="/" method="get" id="myform">
<input name="abc" type="text" value="Hello" id="abc" />
<input name="xyz" type="text" value="" id="xyz" />
</form>

<p><a href="#" id="reset1">Reset Form using JavaScript</a></p>
<p><a href="#" id="reset2">Reset Form using jQuery</a></p>

</body>
</html>

Open in new window

JavaScript

Avatar of undefined
Last Comment
Proculopsis

8/22/2022 - Mon
leakim971

there's no way with jQuery.

check if you like the following :
 $('p a#reset2').click(function() {
  $('#myform').reset();
 });

Open in new window


test page : http://jsfiddle.net/wLhkY/2/
hankknight

ASKER
$('#myform').reset(); gives me an error!  Is there any way to select the form to be reset using jQuery?
SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Proculopsis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Proculopsis

There is an even more simpler solution.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23