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");
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
acedanger
  • 1,197
  • 2
  • 15
  • 34

5 Answers5

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");
dano
  • 5,640
  • 5
  • 27
  • 27
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/

2Yootz
  • 3,971
  • 1
  • 36
  • 31
  • 3
    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: http://jsfiddle.net/06nLLtbb/1/ – jelhan Feb 23 '15 at 15:02
  • 2
    it's about a year after you posted this and i am very happy you did. – arcaderob Feb 25 '15 at 19:54
  • A relevant/related link about destroying the "on-the-fly" dialog element: https://stackoverflow.com/q/2864740/2943403 – mickmackusa Apr 17 '18 at 02:08
4

Use the plus symbol to concatenate strings:

$('#dialog').text('Click on the link to download the file:
' + data);
$('#dialog').dialog("open");
Karmic Coder
  • 17,569
  • 6
  • 32
  • 42
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) {}
           }
        });
});
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
0

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

Check the documentation.

idrumgood
  • 4,904
  • 19
  • 29