Grow your CSS skills. Land your dream job.

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

  • # July 5, 2012 at 11:30 am

    Just wondering if this is possible to do?

    $(div).ready(function(){
    //code
    });
    # July 5, 2012 at 12:31 pm

    .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/

    # July 5, 2012 at 1:42 pm

    Is there a particular reason why you are trying to do that?

    Would this not work:

    $(document).ready(function(){
    $("#your-div").....
    });
    # July 5, 2012 at 1:44 pm

    @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.

    # July 5, 2012 at 1:54 pm

    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

    # July 5, 2012 at 2:07 pm

    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

    # July 5, 2012 at 10:01 pm

    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...
    });
    fwd
    # July 6, 2012 at 8:08 am

    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.

    # July 9, 2012 at 12:50 pm

    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);
    });
    });
    # July 12, 2012 at 8:29 am

    I agree with “SgtLegend” his suggestion is the best practice.

    # July 12, 2012 at 8:31 am

    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?

    # July 12, 2012 at 1:54 pm

    @jcoder – load() isn’t going to do what you want it to.

    See my previous post about using the imagesLoaded plugin.

    # July 12, 2012 at 5:06 pm

    Thanks for all the Great Help Everyone!

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".