14

How can I dynamically set the text of the dialog box that I'm opening? I've tried a few different things but they all respond with an empty dialog box.

Here is my current try:

$('#dialog').text('Click on the link to download the file:
'.data); $('#dialog').dialog("open");
1
  • 3
    ha, too much php before I wrote that; thanks for the sarcasm though, it was helpful! Commented Oct 6, 2009 at 17:03

5 Answers 5

29

For best practice, try putting a div inside your dialog div and appending text to that instead.

<div id="myDialog"><div id="myDialogText"></div></div>

and then setting the text of the internal Div. This make for better separation, so you have

  • a div for dialog manipulation
  • and a div for text display

You can then set the text with

jQuery("#myDialogText").text("your text here");
Sign up to request clarification or add additional context in comments.

Comments

12

Here is an alternative way of creating dialogs on the fly and setting their messages dynamically:

$('<div></div>').dialog({
    modal: true,
    title: "Confirmation",
    open: function() {
      var markup = 'Hello World';
      $(this).html(markup);
    },
    buttons: {
      Ok: function() {
        $( this ).dialog( "close" );
      }
    }   });  //end confirm dialog

See it in action: http://jsfiddle.net/DYbwb/

3 Comments

The div should be destroyed on close, shouldn't it? Also I don't like to set text on open event. You could create a Element, calling dialog and destroy it on close: jsfiddle.net/06nLLtbb/1
it's about a year after you posted this and i am very happy you did.
A relevant/related link about destroying the "on-the-fly" dialog element: stackoverflow.com/q/2864740/2943403
4

Use the plus symbol to concatenate strings:

$('#dialog').text('Click on the link to download the file:
' + data);
$('#dialog').dialog("open");

Comments

1

Here's an example showing dynamic text in a jQueryui dialog box. The text is from an ajax response. The message is shown below (larger than it appears!).enter image description here

$(".info").click(function(event){
    event.preventDefault();
    $id = $(this).attr("id");
    $.ajax({
           type: "POST",
           url: 'my_code.php',
           data: {query:"info", rowid: $id},
           success: function(data) {
                try {
                   obj = JSON.parse(data);
                   var str = '';
                   for (var i in obj) {
                       str += i + ":" + obj[i] + "<br />";
                       if (i == "id") str += "<hr />";
                   }
                   $("#dialog-1").html(str).dialog();
                   $("#dialog-1").dialog('open');
                } catch (e) {}
           }
        });
});

Comments

0

dialog("open"); isn't a valid jquery UI method. (And what Mike said about concatenating with + instead of .

Check the documentation.

Comments

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.