Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Jquery json using the module pattern? Reply To: Jquery json using the module pattern?

#177536
__
Participant

It would probably be a better learning experience if you used a script that worked to start with. There seem to be a lot of variables/ declarations/ logic that you “left out” of your snippet. I have a general idea of what you’re doing, but not the context of how you’re doing it.

In order to make something into a module, it must be basically self-contained. For example:

/* "paint it black" module */
(function( $, $el ){
    $el.each( function(){ 
        $(this).css( 'background-color','#000' ); 
    } );
})( $, $('.elements-to-paint-black') );

Note this module needs two things from “the outside” (the outer (presumably global) scope): jQuery and a jQuery collection of DOM elements. Instead of assuming these are available in the global scope, on the last line we inject them into the module intentionally.

Also, please make sure to use code blocks to format the code you post here on the forums. If you need to share larger amounts of code, it would be a good idea to use a service like codepen.