views:

109

answers:

2

Why does only the third method work?

$('#jqtest').live('load', function() {$(this).html('hi');}); //1

$('#jqtest').load(function() {$(this).html('hi');}); //2

$(window).load(function() {$('#jqtest').html('hi');}); //3


 <div id="jqtest">kldjfglkj</div>
+1  A: 

You can't use the load() function on arbitrary selectors; you can only use it on "any element associated with a URL: images, scripts, frames, iframes, and the window object" (docs). divs don't have an associated URL, so neither of your first two techniques will bind a handler. window does have a URL, so it will call the handler.

You might also also be interested in ready().

kevingessner
A: 

If you're trying to add the HTML "hi" to the element "#jqtest" when the document or window has loaded you're almost there.

$(document).ready(function(){

$("#jqtest").html('hi');

});

This will change the value of "#jqtest" when the document has been loaded. You can also specify other events within the ready() function to only be executed once the page has fully loaded.