Search code examples
jqueryjquery-uijquery-ui-dialog

How do I open a page's content from an Href link to display it inside a JQuery UI Dialog?


I have a link like this and I want to open its content and display it using the code below:

<a class="uimodal" href="/Administration/Contact">Contact</a> 

How can I make that link open the href content and display it inside of a jQuery UI modal dialog?


Solution

  • The best way is to use an Ajax Load operation to retrieve the content into a new element. Then when the data is loaded, call the modal on that element:

    $('a.uimodal').bind('click', function() {
       var $this = $(this);
       var outputHolder = $("<div id='.uimodal-output'></div>");
       $("body").append(outputHolder);
       outputHolder.load($this.attr("href"), null, function() {
          outputHolder.dialog(// whatever params you want);
       });
       return false;
    });
    

    AJAX Load: http://api.jquery.com/load/ Dialog Options: http://jqueryui.com/demos/dialog/

    Note: You could also display the modal while the AJAX page is loading by putting outputHolder.dialog(//...) prior to calling the load method.