jsertx
jsertx

Reputation: 636

Writing a jQuery plugin

Im doing my first plugin in jquery for practising and It won't work, i have seen tutorials and the api documentation and didnt find the problem.

I have a select in html and a span.sl1. and I call it this way:

$("select").selectFunc("onChange");

My plugin code is:

    (function( $ ){
var metodos = {
    init : function(opciones) {   
        var configuracion = {  
            container:$('span.sl1'),
            valor:$(this).val()
        };  
        return this.each(function(){  
            if(opciones){  
                configuracion = $.extend(configuracion,opciones);  
            }  
        });  
    },
    onChange:function(opciones){
        return this.each(function(){
            $(this).bind('change',function(){
                container.html(valor);
            });
        });
    },
    unbind : function (){  
        return this.each(function(){  
            $(this).unbind('mouseover');  
            $(this).unbind('mouseout');  
        });  
    }  
};
$.fn.selectFunc = function( metodos ) {
    if (metodos[method] ) {
        return metodos[ metodo ].apply( this, Array.prototype.slice.call( argumentos, 1 ));
    }
    else if ( typeof metodo === 'object' || ! metodo ) {
        return metodos.init.apply( this, argumentos );
    }
    else {
        $.error( 'Este método ' +  metodo + ' no existe en jQuery.estiloPropio' );
    }    
};
})( jQuery ); 

Upvotes: 1

Views: 389

Answers (1)

index
index

Reputation: 3727

You might also want to check this page for a quick jquery plugin boilerplate.

Upvotes: 1

Related Questions