views:

231

answers:

2

(I have tightened up my original example) I'm trying to invoke modal dialogs from within a tabbed UI, and I'm confused about the behavior I'm seeing. The first time I display the UI, my dialog behaves as expected, I can pull the data out of the fields, everything's wonderful.

tabtest2.html:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tabtest 2</title>

<link rel="stylesheet" type="text/css" href="js/css/smoothness/jquery-ui-1.7.2.custom.css" media="screen"/>  

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function()
{
  var tabs = $('#tabs').tabs({
    load: function(event, ui)
    {
      initializeUI();
    }
  });
});

function initializeUI()
{
  jQuery("#button1").click(function()
  {
     $(initializeUI.win).dialog("open");
  });

  $(initializeUI.win) = jQuery("#window1");

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                            $(initializeUI.win).dialog("close");
                        }
                    }
               });
}
</script>

</head>   

<body>

<div id="tabs">
  <ul>
    <li><a href="tab1.html">Tab1</a></li>
    <li><a href="http://google.com"&gt;Google&lt;/a&gt;&lt;/li&gt;
  </ul>
</div>

</body>
</html>

And tab1.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tab 1</title>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

</head>   

<body>

<button id="button1" class="ui-button ui-state-default ui-corner-all">Button 1</button>

<div id="window1" style="display:none">
  <form>
    <fieldset>
      <label for="text1">Text 1</label>
      <input type="text" name="text1" id="text1" class="text ui-widget-content ui-corner-all" />
    </fieldset>
  </form>
</div>

</body>
</html>

This allows the dialog to (apparently) work on repeated tab selections, but when I try to change the contents of the text field and examine it, I get the old value (the value from the first invocation)!! It's as if I have created a new copy of the dialog and it's fields, but the original text field is sitting there unseen in the original dialog window, returning it's old results.

Obviously, there's a paradigm for handling these dialogs, divs and tabs that I haven't grasped yet. Anyone care to point out my errors?

Much thanks in advance.

+1  A: 

In your example you are using the same remote content twice and more importantly, using the same ID in both tabs. After the content of the second page is loaded into the DOM, you will have two divs with the same ID. Since an ID is supposed to be unique on a page, the "old" values may simple be the values of the first div that javascript happens to find in the DOM.

You also appear to have two buttons with the id "button1"; one inside the modal div and one outside. This may also cause problems.

emills
Thanks, there *was* a fair amount of slop in my example. I have edited my posted code to reflect your concerns, but the underlying issue remains. I appreciate your assistance!
guaguanco
A: 

Using FireBug, I see that I create a new 'dialog' DIV element everytime I call InitializeUI(). So deleting the old DIVs seems to give me the desired results:


function initializeUI()
{
  jQuery("#button1").click(function()
  {
    initializeUI.win.dialog("open");
  });

  initializeUI.win = jQuery("#window1");

  // remove old 'dialog' DIV elements
  $('div[role="dialog"]').each(function() {
    $(this).remove();
  });

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                          initializeUI.win.dialog("close");
                        }
                     }
                   });
}

guaguanco