Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:00:32+00:00 2026-06-16T08:00:32+00:00

I have several forms which look like this: <form id=myForm onsubmit=saveFormData(this);return false> …. </form>

  • 0

I have several forms which look like this:

<form id=myForm onsubmit="saveFormData(this);return false">
....
</form>

Upon submission, a custom function is executed and the default action (sending the data through http) is canceled.

But now I need to validate the form and if it validates ok, then trigger the custom function (saveFormData in this case, but it may be different for other forms), otherwise do nothing.

So the final event handler should work like this:

$('#myForm').submit(function(){
    if(formValidatesOk(this))
        saveFormData() ;
    return false ;
}

However, I cant make changes to the HTML code, so I need a general way of redefining the onsubmit event by wrapping the hardcoded handler with a jquery event handler.

The first thing that comes to my mind is something like (untested):

var hardcodedHandler = $('#myForm').prop('onsubmit') ; // save the current handler
$('#myForm').prop('onsubmit',null) ; // remove the current handler
$('#myForm').submit(function(){
    if(formValidatesOk(this)) // if the form is valid then...
        Function(hardcodedHandler).call(this) ; // trigger the original handler
    return false ;
}) ;

But that’s not very elegant (to say the least).

Do you know of a better way of doing it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:00:33+00:00Added an answer on June 16, 2026 at 8:00 am

    You can just take the old function from the onsubmit property of the element, you don’t need to eval it:

    var form = $('#myForm');
    var oldHandler = form.prop("onsubmit");
    form.removeProp("onsubmit").submit(function(e){
        e.preventDefault();
        if(formValidatesOk(this))
            oldHandler.call(this, e);
    });
    

    Demo at jsfiddle.net

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several forms like this: <table width=100% border=0 cellpadding=0 cellspacing=0 class=table_std> <tr id=exam_String_newValue_row>
I have several forms each having a hidden field that identifies this form...I want
I have several forms inside DIVS on my page. I have one form which
I have several forms on one page which are submitted using ajaxForm and show
I have several DropDown controls on a Web Form which are filled using AJAX
I have a form in which several buttons are added at runtime via a
I have several forms which the only difference is the text in the submit
I have a dataframe in long form for which I need to aggregate several
I have several strings that look like the following: +H124005992014011/1527399999999I05Z +H7039700000001/$99999999I051 +K122005962050171/120911234C117 I need
My form has several <p> elements which are added dynamically. This is how one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.