0

I'm adding some utility functions to jQuery UI dialog, and I need a way to get the selector when attaching functionality to every dialog open:

$(document).on("dialogopen", ".ui-dialog", function (event, ui) { 

What I've found is that in the object, the selector is empty:

selector: "",

And I can't trust that every dialog will be opened by an id, so how do I find the selector on dialogopen?

1 Answer 1

1

In your open callback, event.currentTarget is a reference to the dialog.

So the following will give you the div that is being dialoged.

$(function () {
    $(document).on("dialogopen", ".ui-dialog", function (event, ui) {
        var $div = $(event.currentTarget).find('.ui-dialog-content');

        // Do something with the div
    });

    $('#mydialog').dialog();
});

You can then get the id attribute or use $div.hasClass to determine which div it is.

Or you can add the selector as a data attribute to the div before dialoging it. Check out this jsFiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

The fiddle was exactly what I was looking for. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.