-1

I have a button with a jquery code that clears all data from a from.

<button id="reset">Reset</button>

The jQuery code looks something like this:

$("#reset").click(function()
{
 $(':input').val('');
...LOTS and lots of more code...
 $("#id").focus();
});

ALSO, separete from button...I have an ajax/php, where if successful, I want it to call the $("#reset").click() function.
-I do not want to copy and paste the code.

How can I call that function?
This did not work:

$("#reset").click();

Here is a "dummy sample" of my ajax, just show what I want to do:

//AJAX
//#new
$("#add").click(function()
{
 //START:$.post
 $.post( "ajax.php" ,
 {...var...},
 function(msg)
 {
 if( msg == 1 )
 { //Successful
 ...code...
 $("#reset").click();
 ...more code...
 } //end: if
 else
 { ...else code...} //end: if.else
 }//end: $.post.function
 ); //END:$.post
}); //END:ajax
asked Nov 1, 2011 at 18:30

3 Answers 3

3

You can use .trigger() to trigger an event:

$('#reset').trigger('click');
answered Nov 1, 2011 at 18:32
Sign up to request clarification or add additional context in comments.

Comments

1

Calling $('#reset').click(); "should" work. Are you calling the click before attaching the click event handler? Or maybe you are attaching the event handler to the element before it exists on the page?

answered Nov 1, 2011 at 18:34

Comments

0

Simply calling click() should work. If not, then look at trigger

On a more general note - why would you not use form's standard reset method?

answered Nov 1, 2011 at 18:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.