Reza Sam
Reza Sam

Reputation: 1334

is it possible to set $(this) in function?

I want to set #first and #second as $(this) to use element id only when function is calling. is it possible?or any other way?

 (function( $ ){
        $.fn.showCircle = function(top,right) {
            $timeout(function () {
                $(this).css({
                    right:right,
                    top:top,
                });
            });

        };
    })( jQuery );
    $('#first').showCircle(300,200);
    $('#second').showCircle(800,200);

Upvotes: 1

Views: 71

Answers (2)

Reza Sam
Reza Sam

Reputation: 1334

thanks to @panther comment inside $timeout this refer to window so as @panther said I changed the function :

(function ($) {
        $.fn.showCircle = function (top, right) {
            var self = this;
            $timeout(function () {
                $(self.selector).css({
                    right: right,
                    top: top,
                });
            });
        };
    })(jQuery);

Upvotes: 0

guest271314
guest271314

Reputation: 1

You can use jQuery.proxy() to set this at a function call

$timeout($.proxy(function () {
            $(this).css({
                right:right,
                top:top,
            });
        }, this));

Upvotes: 2

Related Questions