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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:56:59+00:00 2026-06-12T07:56:59+00:00

I am writing a object-oriented jquery plugin. I have a class and I am

  • 0

I am writing a object-oriented jquery plugin. I have a class and I am accessing this class creating an object in the plugin. I would like to access this object without using data of element, because I need to learn “size” option of the element which is specified while calling the plugin.

Some people hold their object on element data like this:

element.data(‘myplugin’, myplugin);

And access this object like this:

$(element).data(‘myplugin’).get_size();

However I would like to use something like this:

$(element).get_myplugin_obj().get_size();

Is it possible to do this? I couldn’t find any example doing this.

Lets say the following code is my plugin. This plugin is called for some input areas. When user writes a letter to an input area, the plugin makes “font-size” of text “hello” bigger. Therefore “hello” message is being bigger and bigger. Another function in giveerror.js looks for the first given size and calculates max size. So this plugin needs to access the given “size” option of each element by using its object of MyPluginClass.

myplugin.js:

 (function($) {

var MyPluginClass = function(element, customOptions)
{
    var elem = $(element);
    var obj = this;
    var options = $.extend({
        size: 1
    }, customOptions || {});

   /** public method **/
    this.addMyPlugin = function() {
        elem.after('<br /><span class="hello" style="font-size:'+options.size+'px">hello</span>');

        calculate(elem);
        elem.change(function() {
            calculate(this);
        });
        elem.keyup(function(){
            calculate(this);
        });
    };

    /** private method */
    var calculate = function(obj){
        var length = $(obj).val().length;
        var newsize = options.size + length;
        $(obj).nextAll('.hello:first').css('font-size',newsize);
        return;
    };

    /** public method to get max size value **/
    this.get_size = function() {
        return options.size;
    };

    /** public method to get object of this class for specific element **/
    this.get_myplugin_obj = function() {
        return obj;
    };


};

$.fn.myPlugin = function(customOptions)
{
    return this.each(function()
    {
        var element = $(this);

        /** create an object for MyPluginClass **/
        var MyPluginClassObj = new MyPluginClass(this, customOptions);

        MyPluginClassObj.addMyPlugin();
    });
};
})(jQuery);

giveerror.js:

  function validate_hello_size_func (element, error_message)
{
        var first_given_size = $(element).get_myplugin_obj().get_size();
        var threshold = first_given_size * 3;
        //var threshold = 10;
        var current_size_px = $(element).nextAll('.hello:first').css('font-size');
        var current_size = parseInt(current_size_px.substr(0,current_size_px.length-2), 10);
        if(current_size > threshold){
            if(!$(element).next().hasClass('error'))
                $(element).after('<span class="error" style="color:red">'+error_message+'</span>');
        } else {
            if($(element).next().hasClass('error'))
                $(element).next().remove();
        }
}

index.php:

<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="myplugin.js"></script>
<script type="text/javascript" src="giveerror.js"></script>
<script type="text/javascript">
$(document).ready ( function ( ) {
    $('#input1').myPlugin({
            size: 5
    });
    $('#input2').myPlugin();

    $('#input1').bind ( 'change', function() {
        validate_hello_size_func (this, 'You exceeded the limit');
    });
    $('#input2').bind ( 'change', function() {
        validate_hello_size_func (this, 'You exceeded the limit');
    });
});

</script>
</head>
<body>
    <div id="div1">
        <input id="input1" type="text" name="input1" value="">
    </div>
    <div id="div2">
        <input id="input2" type="text" name="input2" value="">
    </div>
    <br />
    <input type="submit" name="submit" value="OK">
</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-06-12T07:57:00+00:00Added an answer on June 12, 2026 at 7:57 am

    I solved the problem, I am adding my comments here to help who wants to use same structure, here are the answer:

    Through the return statement:

    • In $.fn.myPlugin, you do the following: return this.each(function() …..
    • In this return function, you need to define this.get_myplugin_obj = function() { return MyPluginClassObj; };
    • This would allow all elements that have access to the get_myplugin_obj function if myPlugin has been called on it. get_myplugin_obj would return the MyPluginClass object, which has the get_size etc. functions

    Here are the new myplugin.js:

    (function($) {
    
        var MyPluginClass = function(element, customOptions)
        {
            var elem = $(element);
            var obj = this;
            var options = $.extend({
                size: 1
            }, customOptions || {});
    
           /** public method **/
            this.addMyPlugin = function() {
                elem.after('<br /><span class="hello" style="font-size:'+options.size+'px">hello</span>');
    
                calculate(elem);
                elem.change(function() {
                    calculate(this);
                });
                elem.keyup(function(){
                    calculate(this);
                });
            };
    
            /** private method */
            var calculate = function(obj){
                var length = $(obj).val().length;
                var newsize = options.size + length;
                $(obj).nextAll('.hello:first').css('font-size',newsize);
                return;
            };
    
            /** public method to get max size value **/
            this.get_size = function() {
                return options.size;
            };
    
        };
    
        $.fn.myPlugin = function(customOptions)
        {
            return this.each(function()
            {
                var element = $(this);
    
                /** create an object for MyPluginClass **/
                var MyPluginClassObj = new MyPluginClass(this, customOptions);
    
                MyPluginClassObj.addMyPlugin();
    
                this.get_myplugin_obj = function() {
                    return MyPluginClassObj;
                };
            });
        };
    })(jQuery);
    

    And get the size with the following function. giveerror.js:

    function validate_hello_size_func (element, error_message)
    {
            var first_given_size = $(element)[0].get_myplugin_obj().get_size();
            var threshold = first_given_size * 3;
            //var threshold = 10;
            var current_size_px = $(element).nextAll('.hello:first').css('font-size');
            var current_size = parseInt(current_size_px.substr(0,current_size_px.length-2), 10);
            if(current_size > threshold){
                if(!$(element).next().hasClass('error'))
                    $(element).after('<span class="error" style="color:red">'+error_message+'</span>');
            } else {
                if($(element).next().hasClass('error'))
                    $(element).next().remove();
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question about using an object-oriented language. I've been using C++ to
I'm writing a class library that provides convenient object-oriented frontends to the C API
I was writing this little piece of code as an exercise in object-oriented programming.
Is this statement true?? Writing object oriented code, even in non-object oriented language. Can
I understand object oriented programming, and have been writing OO programs for a long
I am writing a get function within a class of an object oriented program,
I'm writing an Object Oriented OpenGL framework in perl and I'm running into a
I am new to object oriented programming and writing some of my first classes
I have the following simple methods for writing a python object to a file
I am writing code to clone object but have no cue from Hobo documentation.

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.