javascript - jQuery custom function - this.each -


i need regarding jquery plugin i'm creating.

$.fn.mymethod = {   init : function(options){        this.each(function() {           var settings = {  };           if(options){ $.extend(settings, options); }        });        return this;   } }  $("input[type=text]").mymethod.init(); 

it doesn't seem work, error: typeerror: this.each not function on line

i hope can point me in right direction.

thanks!

you want way without level of object reference:

$.fn.mymethodinit = function(options){        this.each(function() {           var settings = {  };           if(options){ $.extend(settings, options); }        });        return this; } 

when interject object in way in original proposal, means has called as:

$(...).mymethod.init() 

and, calling way, means this pointer mymethod, not jquery object need be. consequence of how javascript sets this pointer when call foo.bar.method().

there work-arounds, ugly , complicated give level of dot syntax. common work-around use common prefix on methods:

$.fn.mymethod_init = function() {...} $.fn.mymethod_save = function() {...} 

which unique namespace attempting use, not create problem ran into.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -