javascript - How To Create methods(functions) in jQuery Plugins -


i want create plugin, looking img file on pc, , after load image file, user can choose methods apply in this. example, in call plugin:

$('#test').myplugin({   legend: true }); 

and code plugin upload file this:

    (function( $ ){ var handlefileselect = function (evt) {     var files = evt.target.files;     for(var = 0, f; f = files[i]; i++) {         if (!f.type.match('image.*')) {             continue;         }         var reader = new filereader();         reader.onload = (function(thefile) {             return function(e) {               // render thumbnail.               var span = document.createelement('span');               span.innerhtml = ['<img class="responsive-img thumb" src="', e.target.result,                                 '" title="', escape(thefile.name), '"/>'].join('');               document.getelementbyid('list').insertbefore(span, null);             };         })(f);         reader.readasdataurl(f);     } }; $.fn.upload = function(options) {     var settings = $.extend( {}, options );     return this.each(function() {         document.getelementbyid('files').addeventlistener('change', handlefileselect, false);     }); }; })( jquery ); 

this code works fine on upload img, don´t have idea, how can add methods in plugin. me? thanks!

how can add methods in plugin

the same way already do. notice how you're adding plugin function here:

$.fn.upload = function(options) {     //... } 

so if want add function different name, add function different name:

$.fn.myplugin = function(options) {     //... } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -