dtryan
dtryan

Reputation: 557

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>

Upvotes: 1

Views: 2344

Answers (2)

darcy
darcy

Reputation: 465

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.

Upvotes: 0

kevingessner
kevingessner

Reputation: 18985

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

Upvotes: 3

Related Questions