Rafael Fragoso
Rafael Fragoso

Reputation: 1309

Error on jQuery Plugin

I'm using jquery 1.9.1 and I'm trying to develop a plugin. The problem is that the plugin isn't working. Here's the code:

    ;(function($) {

    $.fn.single = function() {

        return this.each(function(){

            // Get the instance
            var element = $(this);

            // Resize the "data-target" divs
            element.load(function(){
                changeCSS(element);
            });

            // Bind the method to the resize window event
            $(window).bind("resize", function(){  
                changeCSS(element);  
            });

        });

    };

    // function to resize all the "data-target" divs
    function changeCSS(element) {

        // Grab the screen resolution
        var windowWidth     = $(window).width();
        var windowHeight    = $(window).height();
        // Count how many targets the div has
        var targetsSize     = $("[data-target]").size();

        // Resize the parent div
        $(element).css({
            "width" : windowWidth,
            "height": windowHeight * targetsSize
        });

        // Resize all the targets div
        $(element + "> div[data-target]").each(function(){
            $(this).css({
                "width" : windowWidth,
                "height": windowHeight
            });
        });

    }

})(jQuery);

And I'm calling it on the document like that:

<script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/single-0.1.0.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#single").single();
        });
    </script>

There's no problem in the console. What I'm doing wrong?

Upvotes: 0

Views: 71

Answers (3)

Rafael Fragoso
Rafael Fragoso

Reputation: 1309

Instead of:

element.load(function(){
    changeCSS(element);
});

I did:

changeCSS(element);

And... Instead of:

$(element + "> div[data-target]")

I did:

$(element).children('div[data-target]')

And now the plugin is being executed with no bugs or errors. Thanks Guys!

Upvotes: 0

user1980347
user1980347

Reputation:

Generally it is bad practice to call a function before it is declared. To get your plugin right, you will be better of starting structuring it correctly from the beginning. Besides that, as @mcpDESIGNS point out, you are using the .load method wrongly. Also, it would be useful if you explain what exactly it is you are trying to accomplice here.

To get a good start making jQuery plugins, try to look at the documentation at jQuery here or you can look at this tutorial.

This is the preferred structure:

(function($) {

  // Declare your methods at the beginning of your plugin
  var yourMethods = {
    'methodOne' : function() {

    },
    'methodTwo' : function() {

    }
  };

  $.fn.pluginName = function() {

    return this.each(function() {

    });

  }
}(jQuery));

Upvotes: 0

Mark Pieszak - Trilon.io
Mark Pieszak - Trilon.io

Reputation: 66981

I'm assuming that it is because you are misusing the method .load. If you look at the jQuery docs, it is intended for:

.load() : Load data from the server and place the returned HTML into the matched element.

http://api.jquery.com/load/

Remove the lines element.load(function ..., simply call your changeCSS function, you're already loading this extension on Document.Ready

    return this.each(function () {
        // ...

        changeCSS(element); // <-- just run the function right away

        // ... etc
    });

Upvotes: 2

Related Questions