jqueryjspjquery-easyui

$('#dlg').dialog('close') is not working with simple JSP page


$('#dlg').dialog('close') is not clickable, while on save button alert is coming. The answer on StackOverflow is related to an iframe. It does not work in my case.

<div id="dlg" class="easyui-dialog" title="Complex Toolbar on Dialog" style="width: 400px; height: 200px; padding: 10px" data-options="iconCls: 'icon-save', buttons: '#dlg-buttons'">
  The dialog content.
</div>
<div id="dlg-buttons">
  <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:alert('save')">Save</a>
  <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#dlg').dialog('close')">Close</a>
</div>

On click of close, dialog window should get close.


Solution

  • If you check the console, you should have some error like "cannot call methods on dialog prior to initialization; attempted to call method 'close'"

    Simple fix for this is:

    $('#dlg').dialog().dialog('close');
    

    Demo:

    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    
    <div id="dlg" class="easyui-dialog" title="Complex Toolbar on Dialog" style="width: 400px; height: 200px; padding: 10px" data-options="iconCls: 'icon-save', buttons: '#dlg-buttons'">
      The dialog content.
    </div>
    <div id="dlg-buttons">
      <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:alert('save')">Save</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#dlg').dialog().dialog('close');">Close</a>
    </div>