Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing object of JQuery Plugin Class
    primarykey
    data
    text
    <p>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.</p> <p>Some people hold their object on element data like this:</p> <blockquote> <p>element.data('myplugin', myplugin);</p> </blockquote> <p>And access this object like this:</p> <blockquote> <p>$(element).data('myplugin').get_size();</p> </blockquote> <p>However I would like to use something like this:</p> <blockquote> <p>$(element).get_myplugin_obj().get_size();</p> </blockquote> <p>Is it possible to do this? I couldn't find any example doing this.</p> <p>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.</p> <p><strong>myplugin.js:</strong></p> <pre><code> (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('&lt;br /&gt;&lt;span class="hello" style="font-size:'+options.size+'px"&gt;hello&lt;/span&gt;'); 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); </code></pre> <p><strong>giveerror.js:</strong></p> <pre><code> 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 &gt; threshold){ if(!$(element).next().hasClass('error')) $(element).after('&lt;span class="error" style="color:red"&gt;'+error_message+'&lt;/span&gt;'); } else { if($(element).next().hasClass('error')) $(element).next().remove(); } } </code></pre> <p><strong>index.php:</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Hello&lt;/title&gt; &lt;script type="text/javascript" src="jquery-1.4.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="myplugin.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="giveerror.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(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'); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="div1"&gt; &lt;input id="input1" type="text" name="input1" value=""&gt; &lt;/div&gt; &lt;div id="div2"&gt; &lt;input id="input2" type="text" name="input2" value=""&gt; &lt;/div&gt; &lt;br /&gt; &lt;input type="submit" name="submit" value="OK"&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload