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

$(div).ready(function(){ -- Is this possible?

  • Just wondering if this is possible to do?

    $(div).ready(function(){
    //code
    });
  • .ready() is usually just called on document, because it tells you when the DOM is finished loading. I think what you're looking for is:

    $(div).load(function() {
    // Do stuff
    });
    Here's a link to the API info: http://api.jquery.com/load-event/
  • Is there a particular reason why you are trying to do that?

    Would this not work:
    $(document).ready(function(){
    $("#your-div").....
    });
  • @TheDoc, I'm assuming he's trying to wait for images to load. Otherwise you're right, .ready() would be better majority of the time.
  • I can't seem to get it to work. http://jsfiddle.net/kra3r/

    I thought that's exactly why we have this amazing plugin: https://github.com/desandro/imagesloaded
  • Hm, yeah I read some of the documentation and it looks like it can be pretty buggy. I've only ever used .load() on document to wait for everything to load, so it may not work very well on elements after all. xP
  • Using the .ready() method on the document would suggest all elements within the DOM are ready to be used so using the following is sufficient.

    $(function() {
    // jQuery code here...
    });
  • The philosophy of the ready() event in jQuery is based on the DomReady event of Vanilla Javascript, that is usually thrown by the window object. For other objects, like images, or divs, use the load() event.
  • Hi All,

    Why won't the .load event work? But the Hover Does?

    This Code Works:

    $(document).ready(function() {
    $(".TH").hover(function() {
    $(this).attr("value", "##IVULP##"*1000);
    });
    });


    This Code Doesn't Work:

    $(document).ready(function() {
    $(".TH").load(function() {
    $(this).attr("value", "##IVULP##"*1000);
    });
    });
  • I agree with "SgtLegend" his suggestion is the best practice.
  • I agree with "SgtLegend" using the $function method is good coding practice and one should definitely learn to do things the best way from the start.

    Is there a reason why you want to add a .ready to a div?
  • @jcoder - load() isn't going to do what you want it to.

    See my previous post about using the imagesLoaded plugin.
  • Thanks for all the Great Help Everyone!