wdahab
wdahab

Reputation: 357

Automatically load content into a created div in jquery

I'm looking for a way to have jquery automatically change the content of a <div> upon its creation in a page.

What I'm doing, specifically, is to use jquery to load django templates into the page. Some of those templates contain s that I want to immediately alter as soon as they come into the browser.

As a simple example, what I'd like is for the following to work:

$(".load_into").live("load",function(){$(this).html("Dynamic content!");});

However, "load" isn't an eventtype that can be used with <div>s, so that doesn't actually work. Any thoughts on simple ways to do this? Thanks for any help!

Upvotes: 0

Views: 634

Answers (2)

Nick Craver
Nick Craver

Reputation: 630379

What you probably want here is the livequery plugin, using it would look like this:

$(".load_into").livequery(function(){
  $(this).html("Dynamic content!");
});

Unlike .live() which listens for events, .livequery() looks for new elements, and will run the function you give it on all new elements as it finds them.

Upvotes: 2

Jaro
Jaro

Reputation: 570

you're probably looking for the ready-callback function

$(".load_into").ready(function(){$(this).html("Dynamic content!");});

this will not run after the div is created in the browser the common way would be to tie this callback to the document creation:

$(document).ready(function(){$(".load_into").html("Dynamic content!");});

Upvotes: 0

Related Questions