Home > DeveloperSection > Forums > jQuery options for method stop working
E E Cummings
E E Cummings

Total Post:26

Points:182
Posted on    July-20-2013 8:01 AM

 JQuery JQuery 
Ratings:


 1 Reply(s)
 995  View(s)
Rate this:
Hell developers!

I'm not sure if the question were asked already but I've live example so I hope my question may be useful. Here I've custom plugin for jQuery:

(function($){

    jQuery.alertSay = function(options){

        var options = $.extend({say:'open'}, options};

        alert('We are currently ' + say + '!');

    };

})(jQuery);

connected to the main index.html:

$(document).ready(function() {

    $.alertSay({

        say: 'on vacations'

    });

});

And it doesn't work because of options, cause if I use simple method without any options, like this:

(function($){

    jQuery.alertSay = function(){

        alert('We are currently on vacations!');

    };

})(jQuery);

It works just fine with the link like:

$(document).ready(function() {

    $.alertSay();

});

Due to my poor knowledge in jQuery I'm unable to detect where my mistake is and it would be so nice from you to help me if possible. Thank you!

UPD: Thank you for your replies but unfortunately replacing

alert('We are currently ' + say + '!');

with

alert('We are currently ' + options.say + '!');

change nothing, no alert at all. But I've some errors:

Uncaught SyntaxError: Unexpected token } hallo.js:3

Uncaught TypeError: Object function (a,b){return new p.fn.init(a,b,c)} has no method 'alertSay' (in the html on string $.alertSay({)

thanks in advance



shreesh chandra shukla
shreesh chandra shukla

Total Post:105

Points:735
Posted on    July-20-2013 8:16 AM

Hello!

In your 1st snippet code replace say with options.say

(function($){

    jQuery.alertSay = function(options){

        var options = $.extend( options , {say:'open'}); // some error here

        alert('We are currently ' + options.say + '!');

    };

})(jQuery);


Don't want to miss updates? Please click the below button!

Follow MindStick