1

I currently have a form inside a modal dialog, which has a link to add/edit options in one of the select drop downs. This link opens a new modal dialog on top of the old one as I want. However, I can't seem to get any jquery ui widgets to work inside this second modal dialog (specifically the accordian and datepicker widgets). I have followed How to execute jquery inside a Modal window? and have both the accordian and datepicker widgets working in the 1st modal dialog.

Code I've been trying for 2nd modal dialog (not working):

    $(document).on("click", ".view_dialog_2", function(event) {    
            $dialog_2.load($(this).attr('href'), function()
            {
                $('#accordian').addClass('accordian2');
                $('#meeting_date').addClass('date2');
                $('#follow_up_date').addClass('date2');
                $(function() {
                $( ".accordian2" ).accordion();
                    collapsible: true;
                });
                $(function() {
                    $( ".date2" ).datepicker();
                });
                $dialog_2.dialog('open');
            });
            return false;
        });

Code that is currently working for 1st modal dialog:

    $(".view_dialog").click(function(){
            $dialog.load($(this).attr('href'), function()
            {
                $(function() {
                    $("#addPartNum, .order-button")
                            .button();
                });
                $(function() {
                    $( "#meeting_date" ).datepicker();
                });
                $(function() {
                    $( "#follow_up_date" ).datepicker();
                });
                $dialog.dialog('open');
            });

            return false;
        });

I have tried removing the $(document).on event binding for the 2nd dialog but it just takes me to the linked page w/o any modal dialog. I tried adding the classes because I thought maybe there was a conflict since the datepickers are present in the 1st dialog as well. This is my first project using jquery, and I've been getting it for the most part, but this one has me stumped. Any help would be greatly appreciated :)

EDIT: here is the dialog code for 2nd not working dialog (not sure if necessary or not)

    var $dialog_2 = $("#view_dialog_2").dialog(
        {
            autoOpen: false,
            height: 800,
            width: 800,
            resizable: true,
            modal: true,
            buttons: {
                "Submit": function() {
                    // do stuff 
                    $dialog_2.dialog("close");
                },
                "Cancel": function() {
                    $dialog_2.dialog("close");
                }
            }

        });  

EDIT #2: here is a jsfiddle to kind of demonstrate my problem a bit more: https://jsfiddle.net/8pfjz3k5/

Community
  • 1
  • 1
BSounder
  • 134
  • 9
  • What is `$dialog_2`..? How does the HTML look like..? btw You seen to have lots of document.ready handlers everywhere in your code which is so unnecessary... – T J Feb 18 '16 at 05:48
  • @TJ $dialog_2 is the 2nd modal dialog that I need the accordian and datepicker widgets working in. This is all wrapped in a $(document).ready function.. I didn't realize $(function() was equivalent to another document.ready. Thanks for that! – BSounder Feb 18 '16 at 15:52
  • This is not making much sense. I would suggest making a small working example with http://jsfiddle.net/ and share that url so we can see it all. – Twisty Feb 19 '16 at 00:15
  • @Twisty jsfiddle posted :) – BSounder Feb 19 '16 at 17:25

1 Answers1

0

Might be more than one way to do this, but here is a simple example you can start from: https://jsfiddle.net/7xo1Lcy1/

HTML

<div id="start-box" title="First Form">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Form</p>
  <a id="add" href="#">Add/Edit</a>
  <div id="add-box">
    <label>Next</label>
    <input type="text" />
  </div>
  <script>
    $("#add-box").dialog({
      autoOpen: false,
      resizable: true,
      modal: true,
      buttons: {
        "Submit": function() {
          // do stuff 
          $(this).dialog("close");
        },
        "Cancel": function() {
          $(this).dialog("close");
        }
      }
    });
  </script>
</div>


<a id="start" href="#dialog-conf">Start Here</a>

JQuery

$(function() {
  $("#start-box").dialog({
    autoOpen: false,
    resizable: false,
    height: 340,
    modal: true,
    buttons: {
      "Save": function() {
        $(this).dialog("close");
      },
      Cancel: function() {
        $(this).dialog("close");
      }
    }
  });
  $("#start").button();
  $("#start").click(function() {
    $("#start-box").dialog("open");
  });
  $("#start-box").on("click", "#add", function(e) {
    console.log("Launching Add Box.");
    $("#add-box").dialog("open");
  });
});

So you can see I moved away from $(document) for the .on(). This should look for a Click event just when the dialog is open. It then opens the next dialog (the first still in the background).

I hope that helps.

EDIT

You didn't init the .accordion(). See update to your fiddle: https://jsfiddle.net/Twisty/8pfjz3k5/2/

$("#accordian").accordion();

Make sure your selector is correct and you call the right methods.

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • Thanks for your response. I don't think my question is very clear..sorry. I currently have both dialogs working so that you click a link on page X, it opens page Y in a modal dialog, where there is another link that you can click to open page Z in the next dialog (as your fiddle does). I just need to get the accordian and datepickers working within this last dialog (page Z). I will work on getting a fiddle posted to make it more clear – BSounder Feb 19 '16 at 16:38
  • Please see updated question w/ the jsfiddle based on your starting point. Also, great point in changing $(document).on() to $("#start-box").on() I'd been flooding my js file with $(document).on() bindings so I have some changes to make ;) – BSounder Feb 19 '16 at 17:23