user123_456
user123_456

Reputation: 5795

how to send parameters to jquery plugin function

I want to pass values to jquery plugin function.

This is my plugin:

(function ( $ ) {

    $.fn.myplugin = function(name,value) {
        alert(name + " , " + value )
    };

}( jQuery ));

And this is my call:

$('#wrapper').myplugin({name:'test',value:'big_test'});

I can't receive any data in my plugin. why?

Upvotes: 2

Views: 6483

Answers (2)

sinsedrix
sinsedrix

Reputation: 4775

Just try the following pattern:

    $.fn.myplugin = function(options) {
        var settings = $.extend({}, options);

        alert(settings.name + " , " + settings.value);
    };

Upvotes: 1

bugwheels94
bugwheels94

Reputation: 31920

when you are sending data as object then receive it as object and change your plugin code like

$.fn.myplugin = function(data) {
        alert(data.name + " , " + data.value )
    };

Note:Don't forget to return $(this) object in order to mantain chain ability of jQuery

Upvotes: 5

Related Questions