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

jQuery : Wait till image load before applying function : How

  • How do I get jquery to wait till an image has loaded before it acts. Can I?
  • Wrap your code in this:

    $(window).bind(\"load\", function() { 

    });


    instead of

    $(function() {

    });
  • @chriscoyier: this makes the code 'wait' until the entire page is loaded. I think this is more specific:

    $('#someImageID')
    .load(
    function(){
    //Do stuff once the image specified above is loaded
    }
    );

    Place that piece of code outside your $(function() { }); or $(document).ready(function(){ });, in this way the function above will be executed when just that specific image was loaded, not the entire page.