views:

47

answers:

2

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!

A: 
Jaro
There's no 'ready' for DOM elements.
sje397
Sorry, that was a bit unclear on my part, the div that I want to automatically modify is loaded into the page after $(document).ready() has been called.
wdahab
@sje397: corrected@wdahab: then I don't know of any solutions, sry
Jaro
Heh, thanks anyway
wdahab
+1  A: 

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.

Nick Craver
Thanks, I'll take a look at that, sounds promising.
wdahab
Livequery did exactly what I needed it to do. I'm sure that there's an inefficiency aspect to it, but for my use, it's negligible. Thanks for the advice.
wdahab
@wdahab - Definitely true about your concerns, but it's the best available option I'm aware of, and welcome :)
Nick Craver