Search code examples
javascriptjqueryajaxloadlive

Why can't I get jQuery's live() or load() to work?


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>

Solution

  • 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().