user1
user1

Reputation: 357

Resetting/Clearing a form with jQuery

Trying to clear the form after a submission via button click. The 'clear form' jQuery works in another project I tested but for some reason it wont work here. Both the .validateEngine and .LocalStorage.save functions fire and work properly. Any insight would be appreciated.

function clicked (e) 
{
  if (confirm('Are you sure you want to submit?')) 
    {
       if ($("#formID").validationEngine('validate'))
       {
           my.LocalStorage.save();

        //clear form
            var $form = $("#form");
            $form.find('input:text, input:password, input:file, select, textarea').val('');
            $form.find('input:radio, input:checkbox')
            .removeAttr('checked').removeAttr('selected');

   }
}   
e.preventDefault();  
}

html

<form id="formID" name="myForm">    

 <input class="validate[required]" type="text" id="agree" name="agree"/>         
 <button type="submit" value="Save" id="Save" onclick="clicked(event);">Submit Survey</button>

</form>

Upvotes: 0

Views: 232

Answers (2)

nnnnnn
nnnnnn

Reputation: 150080

You have the form ID wrong. Change:

var $form = $("#form");

to:

var $form = $("#formID");

Also, to save selecting the same form twice it would make sense to move that variable declaration to be the first line inside the if(confirm(...){, like this:

function clicked (e) {
    if (confirm('Are you sure you want to submit?')) {
        var $form = $("#formID");                
        if ($form.validationEngine('validate')) {
            my.LocalStorage.save();    
            //clear form
            $form.find('input:text, input:password, input:file, select, textarea').val('');
            $form.find('input:radio, input:checkbox')
                 .removeAttr('checked').removeAttr('selected');
        }
    }   
    e.preventDefault();  
}

Upvotes: 4

user2897690
user2897690

Reputation:

call $("#formID").reset(); This is standard call in jquery to reset form.

Upvotes: 0

Related Questions