1

I tried binding a named function to a form submit event. At first, I tried binding it to an anonymous function and it worked:

jQuery("#form").submit(function(event){
            event.preventDefault();
            alert("event");
            var reqType = jQuery(this).find('input[name="requestType"]').val();
            alert("requestType:"+reqType);
        });

And it worked fine. But when I used a named function:

         //inside jQuery(document).ready()            
         jQuery("#form").submit(processForm(event));

         //defined outside jQuery(document).ready()
         function processForm(event) {
            event.preventDefault();
            var reqType = jQuery(this).find('input[name="requestType"]').val();
            alert("request type: "+reqType);
        }

Not only does the function did not work as I hoped it would, the alert() would just fire when the page is refresh while the form is not submitted. What could be the cause of this and how do I fix it? Thanks in advance.

1 Answer 1

1

You need to pass the function by reference, without brackets:

jQuery("#form").submit(processForm);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.