views:

32

answers:

2

I'm using the JQuery-UI Accordion, but I'm trying to find a way to not have to initialize it any time any new element is added. Using AJAX I'm inserting html into a page, but on any page load I am having to re-initialize any accordions-

// .ajax handler
success: function(xml) {

    // find accordions
    $("div.accordion").accordion();

}

Is there any way I can automatically have this run any time the DOM has changed? I know there is the livequery plugin -- but is there a simpler more elegant method?

A: 

Someone may be able to provide more information, but I'm running into a similar situation. As far as I can tell, you have to dynamically apply the accordion based upon the changed DOM. You could just use the .live function and have it run on load. Something like this has worked for me.

$('div.accordion').live('load', accordion());

See if that helps you.

JasCav
According to the jQuery docs, that shouldn't work: `load` is only fires for elements with a URL (e.g. images, iframes). I'm curious if and why this works.
kevingessner
Yeah this doesn't work on items added to the DOM. You would think there would be an event for html or domchange.
Daniel Hai
@Daniel - Yeah, that would be useful. I've needed something like that few times. @kevingessner - I'm going from memory...it may have been 'ready'. Either way, this situation isn't exactly the same, but it was similar, so I figured I'd post about it.
JasCav
A: 

I've ended up going with a different approach and tying functionality to mapped strings. I now use one attribute for everything . I do a find() based upon that and load it with it's associated value.

pseudo: $(this)$(this).attr('ui:component');

Daniel Hai