-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 Answer 1

1

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);
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.