Search code examples
javascriptjquerysimplemodal

Problems with manipulating divs within simplemodal dialog popup - Further Edit


Ok, I'm having a major headache with simplemodal - I know I'm almost there, but can't get this to quite work right. I have a simplemodal dialog that has several dynamically created divs inside it, such as

<html>
<head>
<!-- Confirm CSS files -->
<link type='text/css' href='css/confirm.css' rel='stylesheet' media='screen' />
</head>

<body>
<div id='container'>
<h1>Test Page</h1>
<div id='content'>
    <div id='confirm-dialog'>
        Page Content Goes Here
    </div>

<!-- modal content -->
<div id='confirm'>
   <div class='header'><span>Header Text</span></div>
   <div class='message'>
     <div id='optionDiv0'><input type='radio' id='options' name='options' value='0' />Option0</div>
     <div id='optionDiv1'><input type='radio' id='options' name='options' value='1' />Option1</div>
     <div id='optionDiv2'><input type='radio' id='options' name='options' value='2' />Option2</div>
   </div>
   <div class='buttons'>
     <div class='yes'>OK</div>
   </div>
  </div>
</div>
<!-- Load JavaScript files -->
<script type='text/javascript' src='scripts/jquery.js'></script>
<script type='text/javascript' src='scripts/jquery.simplemodal.js'></script>

<script type="text/javascript">
jQuery(function ($) {
$('#confirm-dialog input.confirm, #confirm-dialog a.confirm').click(function (e) {
    e.preventDefault();
    confirm("", function () {
        for(var k = 0; k <= 3; k++) {
            if(options[k].checked) {
            var ele = document.getElementById("optionDiv" + k);
            ele.style.display = "none;";
            //alert("Stop Here");
            }
        }

        });
});
});

function confirm(message, callback) {
$('#confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    containerCss: {
                        height: 300,
                        width: 450,
                        backgroundColor: '#fff',
                        border: '3px solid #ccc'
                  },

    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});
}
</script>
</body>
</html>

In the click code, I'm trying to make it so when the user clicks on one of the radio buttons, then clicks 'OK', that item is no longer visible in the popup. If I follow that code with an alert("Stop here");(shown in the code above, commented out), then I can see the div disappear from the popup. But once I clear the alert box, (or if I comment it out so it never runs), the next time I activate the dialog, the div that I hid is re-appearing. How can I keep it hidden, so that it remains hidden the next time the dialog is activated, or is that possible? Thanks in advance.

FINAL EDIT: Found the solution for the dialog box reverting to its original state every time it opens. I pasted this in just above the jquery code, and it works like a charm:

<script> $.modal.defaults.persist = true; </script>

Found on this site in another thread, as part of a different question. Thanks for all who helped.


Solution

  • Your code still doesn't look complete to me, but as for your confirm function callback

    confirm("", function(){
        $('#confirm input[type=radio]').each(function(){
            if($(this).is(':checked'))
                $(this).parent().empty();
        });
    });
    

    Like that?