0

There are several responses here regarding this question or one similar - they are either obsolete or inaccurate.

I am trying to add my own custom close element to a jQuery UI dialog. I'm guessing I need to either use a delegated handler or be able respond to some event fired by the dialog function.

Obviously $(document).ready() won't work because the dialog happens after the document is loaded.

So - how do go about this? I'm sure I'm missing something pretty simple. (hopefully) Ref: https://api.jqueryui.com/dialog

So far, the feedback I'm getting presumes that there is an element on which I can attach an event handler... It does NOT exist at the time that the script run for the page in question

enter image description here

That works WHEN and only when I run it from a debug window with the dialog box open. When I put that simple command in the script attached to the page... nothing gets attached The command I used in this example is very very simple

jQuery( ".name-group-name" ).click(function() {
  console.log('hello');
});
sea26.2
  • 376
  • 1
  • 5
  • 23
  • 1
    This is discussed in many posts already. Also your post does not include any details about what you want the "custom close element" to be. Another Button? A different Icon for the current button? Please elaborate and include a Minimal, Reproducible Example: https://stackoverflow.com/help/minimal-reproducible-example – Twisty Jun 27 '19 at 01:08
  • As I mentioned... I've seen many posts... none address the issue.. The element doesn't matter... could be a button, could be a div with an 'x' in it.. – sea26.2 Jun 27 '19 at 01:15
  • That doesn't make sense. What do you want it to be? Also still need some sort of example, even if the example doesn't work. What have you tried so far? – Twisty Jun 27 '19 at 01:19
  • An example: https://stackoverflow.com/questions/896777/how-to-remove-close-button-on-the-jquery-ui-dialog This talks about how to target the Close Button in opening. So easy to change it upon `open` event. – Twisty Jun 27 '19 at 01:23
  • I knew this was going to be difficult... https://stackoverflow.com/questions/13176499/jquery-ui-dialog-custom-close-button-x presumes that the script "knows about the target element at the time you run document ready... That isn't the case with the Dialog. Document ready run... I attempt to run something like $( ".selector" ).dialog( "close" ); and it fails; because .selector doesn't even exist when the document is first loaded... Does that make sense? – sea26.2 Jun 27 '19 at 01:30
  • You're not exactly correct. You can modify the elements created when you initialize `.dialog()` and you do that in the document ready event. – Twisty Jun 27 '19 at 01:34
  • Really? Every example I've seen for .dialog required that it is attached to some element existing in the DOM at the time of execution. Therein lies the problem. I"m using

    Profile

    That creates a link that when clicked creates a modal (dialog) that is completely separate from the page from which it is called.
    – sea26.2 Jun 27 '19 at 02:46
  • Then try the `.on("click")` method. – Twisty Jun 27 '19 at 03:10
  • Nope - As I already said... .on("click") requires SOMETHING to attach the handler to. And that something doesn't exist. – sea26.2 Jun 27 '19 at 03:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195600/discussion-between-sea26-2-and-twisty). – sea26.2 Jun 27 '19 at 03:21

2 Answers2

0

It's still not clear what you want and you have provided no example. Here is the basic Dialog Example from https://jqueryui.com/dialog/

First we initialize the Dialog. Once done, we can use the widget method to work with the wrapper. We can select the specific element from the widget and than change it if we want.

$(function() {
  var dlg = $("#dialog").dialog();
  var dlgClose = dlg.dialog("widget").find(".ui-dialog-titlebar-close");
  console.log(dlgClose);
  var newButton = $("<button>", {
    class: "ui-dialog-titlebar-close"
  }).html("Close").button().click(function() {
    dlg.dialog("close");
  });
  dlgClose.replaceWith(newButton);
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

If you just want to change the Icon, you can find the <span> and remove the current ui-icon-closethick class and add your own. If you want to replace the element, you may need to bind a new click event to it.

You can see the element is now:

<button class="ui-dialog-titlebar-close ui-button ui-corner-all ui-widget">Close</button>

Hope that helps.

Twisty
  • 30,304
  • 2
  • 26
  • 45
0

Found the solution. To recap, I'm executing a command that looks like this:

<a href="/admin/config/development/sync/diff/block.block.bartik.search" class="use-ajax" data-accepts="application/vnd.drupal-modal">Show me a modal</a>

The logic and mechanics of how this is working is explained (somewhat) here

The problem is that every example and suggestion has assumed that the jQuery Dialog element was created explicitly with my code. That is not the case. None of solutions offered will work without the following.

Somehow, we have to listen for the event. That is done like this:

// catch dialog event
$(document).on("dialogopen", ".ui-dialog", function (event, ui) {
    //Do some work here
});

That very close to the code example in the jQuery API reference:

$( ".selector" ).on( "dialogopen", function( event, ui ) {} );

The difference is that event is not bound to a non-existent selector (which was exactly the problem I tried to explain. Instead, it is bound to $(document).

Hope this will someone else avoid days of trial and error

sea26.2
  • 376
  • 1
  • 5
  • 23