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 6019267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:23:47+00:00 2026-05-23T03:23:47+00:00

I was attempt to wrap some simple logic into a javascript/jquery closure to bind

  • 0

I was attempt to wrap some simple logic into a javascript/jquery closure to bind a form to jQuery validate. The normal code looks like this …

// attach the jquery unobtrusive validator
$.validator.unobtrusive.parse("#formName");

// bind the submit handler to unobtrusive validation.
$("#formName").data("validator").settings.submitHandler = function() {
    viewModel.Save( $("#formName" ) );
};

Works wonderfully. I just wanted to wrap it and make it cleaner. So I wrote this.

(function ($){
    $.fn.submitHandler = function(callback){
        var container = $(this);
        // attach the jquery unobtrusive validator
        $.validator.unobtrusive.parse(container);

        // bind the submit handler to unobtrusive validation.
        $(container).data("validator").settings.submitHandler = callback(container);
        return true;
    };
})(jQuery);

So the inevitible goal is that I could just do this in the future.

$("#formName").submitHandler(function (e) {
        viewModel.Save(e);
    });

I know it seems silly, but I thought it was a good chance to learn more. I just recently learned about Javascript closures and wanted to try it out, and this felt like a good thing to test it on.

The problem is, if I make an HTML form and try to bind this to it, it does work like I want…but it works twice. First, the form just ‘posts’ when the page loads, then it does the behavior I want and expect after that.

Edit

When I say the form ‘posts’, I mean that the alert dialog in the Save function fires. There does not APPEAR to be any server-post back.

Here is the form I am using to test it on.

<form id="_formName" action="" method="post">
    <input type="text" required="required" />
    <button type="submit">Submit</button>
</form>

<script type="text/javascript">
    var viewModel = {
        Save: function ($form) {
            alert($form.attr('id'));
        }
    };

    $("#_formName").submitHandler(function (e) {
        viewModel.Save(e);
    });

</script>
  • 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-05-23T03:23:48+00:00Added an answer on May 23, 2026 at 3:23 am

    I believe your problem is with this line:

    // bind the submit handler to unobtrusive validation.
    $(container).data("validator").settings.submitHandler = callback(container);
    

    Here you are intending to assign a function reference to the “submitHandler” property… but, what you are actually doing, is calling the function “callback” with a parameter “container”, and assigning the result of that evaluated expression to the “submitHandler” property… which, I don’t believe you were intending to do.

    You might try this instead (keep in mind, I really don’t know the specifics of your situation, i’m just going off what I can deduce):

    // bind the submit handler to unobtrusive validation.
    $(container).data("validator").settings.submitHandler = function() { callback(container); };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an attempt to wrap some unmanaged code in a managed .dll I'm trying
In an attempt to add some parameter validation and correct usage semantics to our
Has there been any attempt and creating a formalized method for organizing CSS code?
When I attempt to use the iterator form of parsing for a Spirit grammar
For some reason when I attempt to make a request to an Ajax.net web
I am using the following code to attempt to read a large file (280Mb)
I made my first jQuery plugin attempt, but I have this problem/bug which I
If I attempt to connect to Mac OS X 10.5 Leopard's built in vnc
When you attempt to declare an unsigned variable in C#.NET with a value outside
I've decided to attempt using the double submitted cookies technique to attempt to prevent

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.