views:

146

answers:

2

Working on a Wicket application that adds markup to the DOM after onLoad via Wicket's built-in AJAX for an auto-complete widget. We have an IE6 glitch that means I need to reposition the markup coming in, and I am trying to avoid tampering with the Wicket javascript... blah blah blah... here's what I'm trying to do:

  1. New markup arrives in the DOM (I don't have access to a callback)
  2. Somehow I know this, so I fire my code.

I tried this, hoping the new tags would trigger onLoad events:

 $("selectorForNewMarkup").live("onLoad", function(){ //using jQuery 1.4.1
    //my code
});

...but have become educated that onLoad only fires on the initial page load. Is there another event fired when elements are added to the DOM? Or another way to sense changes to the DOM?

Everything I've bumped into on similar issues with new markup additions, they have access to the callback function on .load() or similar, or they have a real javascript event to work with and live() works perfectly.

Is this a pipe dream?

A: 

.live() doesn't work like this, it's a common misconception. .live() creates an event handler at the DOM root and waits for events to bubble up to it. If the selector matches the event target, .live() will fire the bound event.

It doesn't look for new objects and bind events to them in any way, rather it just listens for a bubble, and doesn't care when that object was added to the DOM.

You need to fire whatever code is needed to run manually when your load operation completes.

What will this is the livequery plug-in, look specifically at the livequery( matchedFn ) call.

You can do something like this:

$('#myID').livequery(function() { $(this).offset()...stuff });
Nick Craver
Yeah, I think I get the concept - what I was hoping for was that there was an event I didn't know about that fires when a new element is added. You'd think this would be a useful event wouldn't you? My original misconception was that "onLoad" might trigger when the new element is, umm, loaded. Oh well. Thanks for the information!
peteorpeter
@peteorpeter - There is a plugin usually used for what you want called liveQuery: http://docs.jquery.com/Plugins/livequery This will run a function when a new element matching the selector is found, check it out. Basically you can do this: `$('#thing').livequery(function() { $(this).positionMe(); });` Updated the answer with this info :)
Nick Craver
I had assumed all of livequery's functionality had been contained in live(). This looks very nicely suited to this use case. Thanks.
peteorpeter
A: 

i guess this is what you are looking for http://ananthakumaran.github.com/2010/02/19/wicket-post-ajax-handling.html

Anantha Kumaran