Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Why isn't this custom jquery plugin not working?

I have this jquery plugin I'm using to center an element on the page. It works. However, when I wrap the all the code in a function called "run" - it stops working. Why?

(function ( $ ) {


$.fn.centerify = function() {

function run() {

    var element = this;
    var elementHeight = $(element).height();
    var elementWidth = $(element).width();
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();

    $(element).css({
      "position": "absolute",
      "top": windowHeight / 2 - elementHeight / 2,
      "left": windowWidth / 2 - elementWidth / 2,
    });

}

run();

};


}( jQuery ));

$('.myDiv').centerify();

https://codepen.io/Woodenchops/pen/jQLgKB?editors=1011