JQUERY, before you go to bed, put your toys AWAY! 8

Posted by Peter Morris Wed, 10 Feb 2010 15:11:00 GMT

So, I have some code that creates a jquery dialog, and inside the dialog, there is a set of tabs.

This dialog is inserted into the page via an ajax call, and after the elements defining the dialog and tabs, I have a section that builds up the dialog. Something like this. Note, these files are dummy ones, mocked up just to explore the issue.

<html>
    <head>
        <script type="text/javascript" src="/media/scripts/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="/media/scripts/jquery-ui-1.7.2.custom.min.js"></script>
        <script type="text/javascript" src="/media/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
        <script type="text/javascript" src="/media/tinymce/jscripts/tiny_mce/jquery-tinymce.js"></script>
        <link rel="stylesheet" type="text/css" href="/media/stylesheet.css" />
        <link rel="stylesheet" type="text/css" href="/media/styles/jquery-ui-1.7.2.custom.css" />
    </head>
    <body>
        <h1>Test scaffold</h1>
        <button onclick='load_dialog()'>Hi There</button>
        <div id='dialog-area'>
            <p>This is the dialog area, it will be replaced when you click the button</p>
        </div>
        <script language='javascript'>
            function load_dialog()
            {
                $('#dialog-area').load('/media/dialog.html', function(){
                });
            }
        </script>
    </body>
</html>

 

and the dialog.html file looks like this.

 

<div id='dialog' title='Dialog Title'>
    This is the dialog area.
    <div id='tabs'>
        <ul>
            <li><a href='#tabs-1'>First</a></li>
            <li><a href='#tabs-2'>Second</a></li>
            <li><a href='#tabs-3'>Third</a></li>
        </ul>
        <div id='tabs-1'>
            <h1>Tab One</h1>
            <p>This is the content for tab one</p>
        </div>
        <div id='tabs-2'>
            <h1>Tab Two</h1>
            <p>This is the content for tab two</p>
        </div>
        <div id='tabs-3'>
            <h1>Tab Three</h1>
            <p>This is the content for tab three</p>
        </div>
    </div>
</div>
<script language='javascript'>

$(function(){
    alert('in prep dialog');
    $('#dialog-area > #dialog').dialog({modal: true,
                         width: 500,
                         height: 400,
                         autoOpen: false,
                         buttons: {'ok': function(){$(this).dialog('close');
                         }}});
     $("#dialog").bind('dialogopen', function() {
                               /* init tabs, when dialog is opened */
                               $("#tabs").tabs();
                           });
    $("#dialog").bind('dialogclose', function() {
                               $("#tabs").tabs('destroy');
                               $("#dialog").dialog('destroy');
                               $('#dialog').remove();
                           });
                          
                           /* your function to open the dialog */
                           $("#dialog").dialog('open');
   
});
</script>

 

Now, previously I did not have that $('#dialog').remove(); line in there. the dialog was inside the area that was going to get replaced by the ajax call right? so no problem!

 

But NO, it turns out that when jquery creates the dialog it repositions the dialog element within the DOM of the document, and so it is OUTSIDE of the area being replaced by the ajax. So, when I do it AGAIN, the page now has two dialog elements. Having two confuses jquery and breaks all sorts of things. So, removing the dialog after it has been 'destroyed' fixes the issue.

  1. uctsnu about 1 month later:
  2. awdxjsnwdc about 1 month later:
  3. liana about 1 month later:
    life insurance quotes =]]] home insurance gnyob cheap auto insurance 068 life insurance 646391
  4. azadirachta about 1 month later:
    car insurance quotes 547687 cheap auto insurance 783 home insurance 58339 health insurance quotes 958254
  5. lubov about 1 month later:
    cheap car insurance 6926 home insurance tgv home insurance 20289
  6. sidenav about 1 month later:
    life insurance vmp auto insurance quotes 596201 cheap health insurance qzkad
  7. gdfqvycrc about 1 month later:
  8. standaardtabel about 1 month later:
    health insurance quotes :PPP auto insurance 24653 life insurance zrc home insurance 6290