1

I have a jQuery dialog, and I use the open callback to load some data into via AJAX.

For example:

$('#dialog').dialog({
    modal: true,
    autoOpen: false,
    open: function(){
        $('.content', this).load('/path/to/file', function(){
            // even more code
        });
        // more code
    }
});

While the dialog is already open, I want to re-load the data via AJAX. I figured that I could just trigger the open function I bound above. I read in the docs that you can bind to that open event using $(".selector").bind("dialogopen", function(event, ui){}), so I figured I could trigger the event that way too.

I tried $('#dialog').trigger('dialogopen'), but nothing happened. How can I trigger the open event of a jQuery dialog?

Currently, I figured out that I could use $('#dialog').dialog('option', 'open')(), but that's ugly, there's gotta be a better way!

3 Answers 3

3

You can rewrite what you have as like $('#dialog').dialog('option')['open']() (ugly again)

Which can be beautified as,

var $dOpt = $('#dialog').dialog('option');

and then

$dOpt.open();

DEMO: http://jsfiddle.net/bj4hK/2/


I think you are looking for $('#dialog').dialog('open') http://jsfiddle.net/bj4hK/

Other dirty trick is to close and reopen. $('#dialog').dialog('close').dialog('open') <- However this would mess up if you have some code onclose

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

3 Comments

That doesn't seem to work if the dialog is already open. That's what I want. jsfiddle.net/NTICompass/bj4hK/1
Hmm, I didn't realize $('#dialog').dialog('option') returned an object.
Uglyfied but prevents an error when dialog not exists: ($("#dialog").dialog('option')['close'] || function(){})();
1

why dont you just extract the function in the open event and reuse it wherever you want to?

$('#dialog').dialog({
    modal: true,
    autoOpen: false,
    open: function(){
        loadfunction()
    }
});

function loadfunction(){
  $('.content').load('/path/to/file', function(){
            // even more code
        });
        // more code
}

so you can use the loadfunction everywhere you want to.

2 Comments

That's a good idea. Definitely cleaner than $('#dialog').dialog('option', 'open')().
mm nice one.. I would simply do open: loadfunction incase if you don't have any extra code inside dialog-open
0

In jQuery UI, methods are invoked by calling the widget name and supplying the method name as the argument. So you can do:

$('#dialog').dialog("open");

4 Comments

That doesn't seem to work if the dialog is already open. That's what I want. jsfiddle.net/NTICompass/bj4hK/1
If the dialog is already open, you would have done this at the time it was opened. If you want to take some action regardless of whether the dialog is already open, write a function that performs this action and calls .dialog("open").
What I want to do is with the dialog open, trigger the open function, so that it reloads the AJAX data.
Best is probably the method in nWorx's answer.

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.