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

How to create a function?

  • I'd like to make my jQuery a bit more smart by creating functions instead of repeating myself over and over. Take this code as an example:

    $('#button').click(function(){
    $('#box1').animate({width: 300, height: 250}, 1000);
    $('#box1').animate({opacity: 0.75}, 2000);
    });


    Now, I'd like to do the very same two animations for another object plus I need the #button (what do you call this element in this example? Trigger?) to be something else. How will I do this best?

    Thanks,
    Oskar
  • Have you seen the screen-casts on jQuery? I think the second one could help you out. Chris shows you how to make you own functions. I highly recommend it for solving your problem, if you haven't already watched it.

    http://css-tricks.com/video-screencasts ... -jquery-2/

    Check it out.