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

The Archive Base Latest Questions

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

How is it possible to give a jquery-plugin individual local variables, that are accessable

  • 0

How is it possible to give a jquery-plugin individual local variables, that are accessable in different plugin-functions?

My script shows an alert with the content ‘123’, but I am expecting ‘abc’. So variable ‘t’ exists only once and not twice for each plugin. So for each plugin-instance there should be also an instance of variable ‘t’.

<html>
<head>
<title></title>

<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>

<script type="text/javascript">
    (function ($) {
        var t = null;
        $.fn.doSomething = function()
        {
            alert(t);
        }
        $.fn.myHtmlControl = function(option) {
            t = option;
        }
    })(jQuery);

    $(function () {
        $('#ctrl1').myHtmlControl("abc");
        $('#ctrl2').myHtmlControl("123");            
        $('#ctrl1').doSomething();
    })        
</script>

</head>
    <body>
        <div id='ctrl1'>Ctrl1</div>
        <div id='ctrl2'>Ctrl2</div>
    </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-27T02:47:23+00:00Added an answer on May 27, 2026 at 2:47 am

    The usual way to do this is to use the data function to store your information related to a specific element on the element itself. So in your case (live example):

    (function ($) {
        $.fn.doSomething = function()
        {
            alert(this.data("myHtmlControl"));
        }
        $.fn.myHtmlControl = function(option) {
            this.data("myHtmlControl", option);
        }
    })(jQuery);
    

    If you need to store multiple options, here’s a more robust example (live copy):

    (function ($) {
        var defaults = {
            msg1: "(msg1)",
            msg2: "(msg2)"
        };
    
        $.fn.doSomething1 = function()
        {
            alert(getOption(this, "msg1"));
            return this;
        }
        $.fn.doSomething2 = function()
        {
            alert(getOption(this, "msg2"));
            return this;
        }
    
        $.fn.myHtmlControl = function(options) {
            this.data("myHtmlControl", $.extend({}, defaults, options));
            return this;
        };
    
        function getOption(inst, name) {
            var obj = inst.data("myHtmlControl");
            return (obj || defaults)[name];
        }
    
        function setOption(inst, name, value) {
            var obj = inst.data("myHtmlControl");
            if (!obj) {
                obj = $.extend({}, defaults);
                inst.data("myHtmlControl", obj);
            }
            obj[name] = value;
        }
    })(jQuery);
    jQuery(function($) {
    
        $("#theButton").click(function() {
            $('#ctrl1').myHtmlControl({msg1: "abc"});
            $('#ctrl2').myHtmlControl({msg2: "123"});
            alert("about to do ctrl1");
            $('#ctrl1').doSomething1().doSomething2();
            alert("about to do ctrl2");
            $('#ctrl2').doSomething1().doSomething2();
        });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to give individual cells in a data grid view row different
What is the best possible way using jQuery to give height and width to
Is it entire possible to run Facebook SDK code integrated into a jQuery script?
Possible Duplicate: javascript (jquery) calendar plugin I am going to develop the weekly calendar
Is it possible to show and hide partially with jquery sliding functions? Basically, there's
Is it possible to write a function in jQuery/Javascript of our own so that
It is apparently not possible to give inividual <option> tags different font-family values. I
I can not add button to this jquery ui dialog. if possible please give
can i perform select single node from IXMLDOMElement ,if its possible give me one
I was wondering if it is possible to give people something when they join

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.