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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:25:47+00:00 2026-05-19T00:25:47+00:00

For a JQuery plugin that I am trying to write I would like to

  • 0

For a JQuery plugin that I am trying to write I would like to store a local variable that whose scope is limited to the HTML identified by the selector. In the basic example below an HTML page has two form elements, I select on each form element separately and inside each instance I want to have the count of the number of text elements the form contains. In the example below “_elements” always contains the number of text input elements for last selector called. Now I think this is because of Javascript’s block level scope and closure, but not sure how to implement it. Could somebody please show me the error of my ways.

Please don’t suggest different ways of doing this (i.e. not storing the variable at all, always selecting the count of the matching elements directly – I know that would work but this is just a simplified example of the problem I’m having with local variables

<html>
    <head>
        <script language="javascript" src="jquery-1.4.4.min.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">

            (function ($) {

                var methods = {

                    Setup: function (options) {
                        if ($(this).length == 0)
                            throw new Error("AutoSave() selector did not find required element");
                        if ($(this).length > 1)
                            throw new Error("AutoSave() is not designed to handle a selector that matches two or more elements!");

                        _elements = $('INPUT[TYPE=TEXT]', this)
                        _elements.length;

                        return this;
                    },
                    NumOfElements: function (options) {
                        alert(_elements.length);
                        return this;
                    }
                };

                $.fn.autoSave = function (method) {

                    if (methods[method]) {
                        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                    } else if (typeof method === 'object' || !method) {
                        return methods.Setup.apply(this, arguments);
                    } else {
                        $.error('Method ' + method + ' does not exist on jQuery.AjaxClickEvent');
                    }
                };

            })(jQuery);

        </script>
    </head>
    <body>
        <form method="post" action="" id="myForm" name="myForm">
            <input type="text" id="txt1" name="txt2" />
            <input type="submit" id="submit" name="submit" />
        </form>
        <form method="post" action="" id="myForm2" name="myForm2">
            <input type="text" id="Text1" name="Text1" />
            <input type="text" id="Text2" name="Text2" />
            <input type="submit" id="submit1" name="submit1" />
        </form>
<script>
    $(document).ready(function () {
        $("#myForm").autoSave();
        $("#myForm").autoSave('NumOfElements');  // Shows 1
        $("#myForm2").autoSave();
        $("#myForm2").autoSave('NumOfElements');  // Shows 2
        $("#myForm").autoSave('NumOfElements');  // Shows 2 (should be 1)
    });
</script>
    </body>
</html>
  • 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-19T00:25:48+00:00Added an answer on May 19, 2026 at 12:25 am

    OK so your code is fine, you’re not crazy. The problem is that this in the methods.Setup function is not the jQuery object/html node. It is the methods singleton. So you are always modifying the same _elements – hence 2 in the last example.

    The solution is to add:

    var methods = {
      my_form: null,
      ...
    }
    

    And then when it all gets instantiated:

    $.fn.autoSave = function (method) {
      methods.my_form = this;
    

    Then change the this calls in your methods to reference methods.my_form. Then you will have the appropriate object reference to the DOM. (I would also suggest storing _elements on the HTML node in the DOM and not “out in the ether” – use data http://api.jquery.com/data/)

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

Sidebar

Related Questions

I am trying to write a jQuery plugin, that can be applied to a
i'm trying to write a jquery plugin that when i click on a image
I have been trying to make this to be a little jQuery plugin that
I was unsuccessful in finding a jQuery plugin that allowed me to write in
I learned that by trying to use the tablesorter plug in from jquery the
I'm playing around with writing a jQuery plugin that uses an attribute to define
I am wondering if anyone has any experience using a JQuery plugin that converts
I'm looking for a plugin for jQuery that can validate as a key is
I am using the jQuery tableSorter plugin on a page. Unfortunatley, the table that
I'm currently working on a jQuery plugin and I'm wondering if it would be

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.