"this" means something different inside every function.

In your default success() function, what do you want "this" to refer to? I
assume you want it to be the jQuery object that called your plugin, right?
Then you might do something like this (pun intended):

    $.fn.poll = function(options){
        var $this = this;
        // extend our default options with those provided
        var opts = $.extend({}, {
            type: "POST",
            url: ".",
            success: function(data){
                $this.html(data)
            },
            interval: 2000
        }, options );
        setInterval(update, opts.interval);
        
        // method used to update element html
        function update(){
            $.ajax({
                type: opts.type,
                url: opts.url,
                success: opts.success
            });
        };
    };

That doesn't address what you may have wanted to do with the default
options, but it may help point you in the right direction.

-Mike

> From: Seth Buntin
> 
> I am trying to create a polling plugin called via $ 
> ("#element").poll();
> 
> Within the plugin I am calling the $.ajax function.  I have a 
> default success option within the plugin but also allow the 
> developer to override if needed.
> 
> The problem I am having is that withing the success of the 
> ajax function I have:  $(this).html(data).  That isn't being 
> called and I can't figure out why.
> 
> Here is my code:
> 
>     $.fn.poll = function(options){
>         // extend our default options with those provided
>         var opts = $.extend({}, $.fn.poll.defaults, options);
>         setInterval(update, opts.interval);
> 
>         // method used to update element html
>         function update(){
>             $.ajax({
>                 type: opts.type,
>                 url: opts.url,
>                 success: opts.success
>             });
>         };
>     };
> 
>     // default options
>     $.fn.poll.defaults = {
>         type: "POST",
>         url: ".",
>         success: function(data){
>             $(this).html(data)
>         },
>         interval: 2000
>     };
> 
> Can someone help me?
> 

Reply via email to