treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Jquery all code under $(document).ready(function()

  • Ok so i know code needs to be under this:

    $(document).ready(function(){
    code for hightligning a paragraph with a particular class
    });

    $(document).ready(function(){
    code for doing animation for menu
    });


    Question if i have differnt code for different uses do i need to split it up, is there a problem if i want to split/put it together in the js file.?
    can i do it like this:

    $(document).ready(function(){
    code for hightligning a paragraph with a particular class
    code for doing animation for menu
    code for doing slider
    code for doing accordion
    });

    i'm really new to jquery so it might be a silly question.
  • You can :)
  • Yes you can do that. You might want to look into making your code a little more "object oriented" by breaking up specific tasks into objects. For example you could do:



    var animation = function(){
    //animation code
    }

    var slider = function(){
    //slider code
    }

    var accordion = function(){
    //accordion code
    }

    var highlight = function(){
    //highlight code
    }

    $(document).ready(function(){

    var animationObj = new animation();
    var sliderObj = new slider();
    var accordionObj = new accordion();
    var highlightObj = new highlight();

    })


    This will better organize your code and as you start to develop more advanced scripts it will help you make your code run more efficiently.
  • Thank you ! I get it now.