views:

33

answers:

1

Please take a look at the following:

jQuery.fn.jqPos = function(target, settings) {
    settings = jQuery.extend({
        offset: [ 0, 0 ]
    }, settings);

    return this.each(function() {
        magic($(this), target, settings);
        $(window).resize(function(){
            magic($(this), target, settings);
        });
    });

    function magic(self, target, settings) {
        // Here I position self close to target
    }
};

This works perfectly when I first initialize the plugin, like $('div#one').jqPos($('div#two')); and the magic method runs as it should. But at the event window.resize nothing happens at all (I want it to run the same method with the same settings and parameters)!

How come? And how to overcome?

EDIT: In the magic-method (at window.resize), the arguments are all undefined.

+1  A: 

Youre confusing what this refers to in your $(window).resize(function(){ magic($(this), target, settings); }); this no longer refers to your element but rather the window itself. try:

 return this.each(function() {
        var $this = $(this);
        magic($this, target, settings);
        $(window).resize(function(){
            magic($this, target, settings);
        });
    });
prodigitalson
lol of course. Talk about brainfart. Thanks m8!
Mickel