Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Dialog Box

Im trying to do a dialog box with jquery. In this dialog box Im going to have terms and conditions. The problem is that the dialog box is only displayed for the FIRST TIME.

This is the code.

JavaScript:

function showTOC() {     $("#TOC").dialog({          modal: true,          overlay: {              opacity: 0.7,              background: "black"          }      }) } 

HTML (a href):

<a class="TOClink" href="javascript:showTOC();">View Terms & Conditions</a>  <div id="example" title="Terms & Conditions">1..2..</div> 

The problem I think is that when you close the dialog box the DIV is destroyed from the html code therfore it can never be displayed again on screen.

Can you please help!

Thanks

like image 223
David Bonnici Avatar asked Dec 14 '08 16:12

David Bonnici


People also ask

How to Use JQuery dialog box?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.

How to open a dialog in JQuery?

Syntax: $( ". selector" ). dialog("open");

What is a JQuery modal window?

JQuery Modal is an overlay dialog box or in other words, a popup window that is made to display on the top or 'overlayed' on the current page. It is a pop up modal box that is converted into the viewport deliberately for the user to interact with before he can return to the actual site.


2 Answers

I encountered the same issue (dialog would only open once, after closing, it wouldn't open again), and tried the solutions above which did not fix my problem. I went back to the docs and realized I had a fundamental misunderstanding of how the dialog works.

The $('#myDiv').dialog() command creates/instantiates the dialog, but is not necessarily the proper way to open it. The proper way to open it is to instantiate the dialog with dialog(), then use dialog('open') to display it, and dialog('close') to close/hide it. This means you'll probably want to set the autoOpen option to false.

So the process is: instantiate the dialog on document ready, then listen for the click or whatever action you want to show the dialog. Then it will work, time after time!

<script type="text/javascript">          jQuery(document).ready( function(){                    jQuery("#myButton").click( showDialog );              //variable to reference window             $myWindow = jQuery('#myDiv');              //instantiate the dialog             $myWindow.dialog({ height: 350,                 width: 400,                 modal: true,                 position: 'center',                 autoOpen:false,                 title:'Hello World',                 overlay: { opacity: 0.5, background: 'black'}                 });             }          );     //function to show dialog        var showDialog = function() {         //if the contents have been hidden with css, you need this         $myWindow.show();          //open the dialog         $myWindow.dialog("open");         }      //function to close dialog, probably called by a button in the dialog     var closeDialog = function() {         $myWindow.dialog("close");     }   </script> </head>  <body>  <input id="myButton" name="myButton" value="Click Me" type="button" /> <div id="myDiv" style="display:none">     <p>I am a modal dialog</p> </div> 
like image 98
RaeLehman Avatar answered Sep 28 '22 05:09

RaeLehman


Looks like there is an issue with the code you posted. Your function to display the T&C is referencing the wrong div id. You should consider assigning the showTOC function to the onclick attribute once the document is loaded as well:

$(document).ready({     $('a.TOClink').click(function(){         showTOC();     }); });  function showTOC() {     $('#example').dialog({modal:true}); } 

A more concise example which accomplishes the desired effect using the jQuery UI dialog is:

   <div id="terms" style="display:none;">        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.    </div>    <a id="showTerms" href="#">Show Terms &amp; Conditions</a>          <script type="text/javascript">        $(document).ready(function(){            $('#showTerms').click(function(){                $('#terms').dialog({modal:true});               });        });    </script> 
like image 22
carlsz Avatar answered Sep 28 '22 03:09

carlsz