4

I am implementing sticky notes using jquery ui dialog boxes. On a button click a big full screen ui dialog box is opened and inside that big dialog box is a button to add small dialog boxes (notes).

HTML:

<body>

  <button id="opener">open the dialog</button>

  <div id="outter-dialog" title="Notes">
    <button id = "add-note">Add Note</button>
  </div>

JS:

$( "#outter-dialog" ).dialog({
        autoOpen: false,
        title: "Success Message",
        width: $(window).width(),
        height: $(window).height(),
        modal: false,
    buttons: {
        Cancel: function() {
            $(this).dialog('close');
        }
    }
});

$("#opener").click(function(){
    $( "#outter-dialog" ).dialog('open');
});


var prevelement;
$("#add-note").click(function () {
    var dynamicDialog = $('<div id="MyDialog"> <textarea>Hello</textarea> </div>');
    var pos;
    if (prevelement) {
        pos = {
            my: "left",
            at: "bottom",
            of: prevelement
        }
    }

    dynamicDialog.dialog({
        title: "Note",
        modal: false,
    appendTo: "#outter-dialog",
        buttons: [{
            text: "Save",
            click: function () {}
        }],
        position: pos
    });
    prevelement = dynamicDialog
});

Now I have an issue when I add appendTo: "#outter-dialog" to my dynamic small dialog boxes (notes):

after appending them to outter dialog box they are no longer resizable and draggable.

I appended them to outter dilaog box so that on open/close of outter dialog box inner notes are shown/hidden.

Any idea why they are not draggable and resizable??

shas
  • 703
  • 2
  • 8
  • 31
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • 1
    you have to delegate the event for dynamically generated html see here http://stackoverflow.com/questions/22960144/jquery-draggable-not-working-for-appended-table-row – Kartikeya Khosla Sep 15 '14 at 05:44
  • @Kartikeya: I tried `$( "#outter-dialog" ).on( "mouseover", ".MyDialog", function( event ) { var elem = $( this ); alert(elem.text()); elem.dialog("option", "resizable", true); }); ` . But still it's not resizable. Any idea?? – Siddharth Trikha Sep 15 '14 at 09:03

2 Answers2

8

Here is the way you can overcome the issue, Demo Just set the dragable and resizable to false and call them separately once you create the dialog. and remember to clone the dynamic element and use dynamic id for that element

var elementCount = 0;
$("#add-note").click(function () {
    var dynamicDialog = $('<div> <textarea>Hello</textarea> </div>');
    var pos = {
        my: "left",
        at: "bottom",
        of: "#dialog" + elementCount
    }

    var dialogId = "dialog" + elementCount;
    dynamicDialog.clone(true).attr("id", dialogId).dialog({
        title: "Note",
        modal: false,
        appendTo: "#outter-dialog",
        draggable: false,
        resizable: false,
        buttons: [{
            text: "Save",
            click: function () {}
        }],
        position: pos
    });
    $("#" + dialogId).parent().draggable().resizable();
    elementCount++;
});
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
-2
$( "#outter-dialog" ).dialog({
autoOpen: false,
title: "Success Message",
width: $(window).width(),
height: $(window).height(),
modal: false,
resizable: true,
draggable: true,
buttons: {
        Cancel: function() {
         $(this).dialog('close');
        }
    }
});