views:

76

answers:

2

I have a form, this form has a submit and a cancel button. when you press submit the following jquery will run:


  $('#saveAction').click(function() {
      // call validation function in javascript

  });



and when cancel is pressed




  $('#cancelAction').click(function() {
    return true;
  });



the reason to do this is to skip javascript validation and let the action= "page.php" do the rest of the job.

Now I am working in an older system that has a very complex validation functions write in javascript, this validation funcitons receive as a parameter the form. since I am going from jQuery to javascript how do I pass from jQuery the form that javascript will understand?

Thank you

+1  A: 

It probably needs the actual DOM element.

Try passing document.formName where formName is the name attribute of the form.

Alternatively, pass document.getElementById("formId"). Either will give you the actual DOM element. $('#formId') will give you a jQuery object.

Stefan Kendall
This worked great!return(EventValidateForm(document.getElementById('formId')));
Onema
+1  A: 

Rather than performing your validation on the click of the submit button, you should really perform the validation when a user submits the form (as they may do so by pressing enter within a text field as well as clicking the submit button). This makes it easy to pass the form to the javascript function too, as follows:

$('form').submit(function(){ old_validation_function(this); })

this is the form object that has been submitted.

Phil
I couldn't use this because I do not want to submit the form just yet, and if cancel is pressed I want to skip validation altogether.
Onema
You can cancel the form submission if validation fails this way. But you can't validate the form if it is submitted in any other way than clicking the submit button if you just use the click handler.
Phil