Problems with UI dialogs if the page gets dynamically loaded more than once
Hello all,
The project I am working on dynamically loads some of it's content via AJAX into a "cont" div. The function to do this is something like this :
- $('#listnav a').each(function(i, el) {
- $(el)
- .click(function(ev) {
- ev.preventDefault();
- var href = $(this).attr('href');
- $('#cont').fadeOut(400, function() {
- $.ajax({
- url:href,
- success:function(html) {
- $('#cont').html(html);
- },
- error:function(xhr, text, error) {
- $('#cont').html(text);
- },
- complete:function() {
- $('#cont').fadeIn(400);
- }
- });
- });
- });
- });
This makes the href of the links in the listnav load into the cont div rather than going to a new page. I have run into some problems with UI dialog controls on these pages. If I just go to a page once the dialog works fine, but if I click on the link for the page more than one time without doing a complete page refresh my dialogs behavior becomes unpredictable. I believe this is because the dialog is being created and all it's functions are being defined in the document ready event of the page, so they are getting applied multiple times to the same element and messing things up. Could anyone give me some advice on how to handle this? How is this type of thing usually done? Any advice is really appreciated. Thanks much!