1

I have a jQuery Dialog popup in which I can review and add items to the existing content. The new content is being saved as soon as I click the SAVE-Button.

<div id="container">
  <ul>
    <li>Elem 1</li>
    <li>Elem 2</li>
  </ul>

  <button id="btnAdd">ADD</button>
  <button id="btnSave" value="submit">SAVE</button>
</div>

<button id="openModal">Open dialog</button>

---------------------------------------------

$(function() {
    $( "#container" ).dialog();
});

$('#openModal').click(function() {
    $( "#container" ).dialog();
});

$('#btnAdd').click(function() {
    $( "#container ul" ).append( "<li>NEW Elem</li>" );
});

$('#btnSave').click(function() { /* ignore */ });

My problem now is, that the content does not get kind of setback when the user clicks the "X" (close) in the titlebar. I want to get back to the original state when the dialog was first opened and before the new items have been added.

How can this be resolved?

Fiddle-Demo: https://jsfiddle.net/SchweizerSchoggi/vsd1qpLg/

EDIT: I've updated the sample. This is just simplified. I am not able to add a class or Id to the new li's.

2
  • Where you are adding the class or id to new li's?
    – John R
    Commented Feb 11, 2016 at 10:32
  • Nowwhere, I am not able to do this. My first sample was not that clear regarding this, that's why I've updated question and sample without IDs and classes.
    – JonSnow
    Commented Feb 11, 2016 at 10:49

3 Answers 3

1

Just add this function in your jquery code

var numlist = $('li').length;

$( "#container" ).on( "dialogclose",function(event,ui){
     var count = 1;       
     $('li').each(function(){
       if(count>numlist)
       {
           $(this).remove();
       }
       count++;
     }); 
});

Or you can use this

var numlist = $('li').length;

$( "#container" ).on( "dialogclose",function(event,ui){
     var finallist = $('li').length;   
     $('li').slice( numlist,finallist).remove();
});
4
  • makes sense. Unfortunatly this is just for demonstration. In the real project I don't have any "markers" like this - unfortunatly. How can I find out which items have been added?
    – JonSnow
    Commented Feb 11, 2016 at 9:58
  • This looks good so far. i am able to identify the length of inital items and the length o items on Close. I just cannot get them removed (in real life its a tr not div that has to be removed)
    – JonSnow
    Commented Feb 11, 2016 at 14:55
  • Instead of li you can use tr and this should work fine. Please accept it as answer if you find it helpfull. Commented Feb 15, 2016 at 4:57
  • Accepted solution, as it got me back on the right track! Thx!
    – JonSnow
    Commented Feb 16, 2016 at 7:20
0

Hope you need something like this.

Store the saved li's in separate global variable.

ulhtml = $("#container ul").html();

Append the saved li while opening the model.

$("#container ul").html(ulhtml);

FIDDLE DEMO

var ulhtml;
$(function () {
    ulhtml = $("#container ul").html();
    $("#container").dialog();
});
$('#openModal').click(function () {
    $("#container ul").html(ulhtml);
    $("#container").dialog();
});

$('#btnAdd').click(function () {
    $("#container ul").append("<li class='green'>NEW Elem</li>");
});

$('#btnSave').click(function () {
    ulhtml = $("#container ul").html();
});
0

Handle the beforeClose event:

$(function () {
  // save the dialog original content
  var innerHtml = $("#container").html();
  $("#container").dialog({
    autoOpen: false,
    beforeClose: function(event, ui) {
      // restore the dialog original content
      $("#container").html(innerHtml);
    }
  });

  $('#openModal').click(function(e) {
    $( "#container" ).dialog('open');
  });
});

// the event delegation for buttons inside the dialog change in:
$(document).on('click', '#btnAdd', function() {
  $( "#container ul" ).append( "<li class='green'>NEW Elem</li>" );
});

$(document).on('click', '#btnSave', function() {
});
#container { padding: 10px; border: solid 1px red; margin-bottom: 30px; }
#container button { margin-top: 20px; }

#container ul li.green { color: green; }
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>


<div id="container">
    <ul>
        <li>Elem 1</li>
        <li>Elem 2</li>
    </ul>

    <button id="btnAdd">ADD</button>
    <button id="btnSave" value="submit">SAVE</button>
</div>

<button id="openModal">Open dialog</button>

2
  • sorry, my fault. The class .green in this example is just for Demonstration purpose. In the real project I don't have any "markers" like this - unfortunatly. How can I find out which items have been added?
    – JonSnow
    Commented Feb 11, 2016 at 10:02
  • @SchweizerSchoggi snippet updated as asked. Let me know
    – gaetanoM
    Commented Feb 11, 2016 at 10:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.