3

Is there a way to open a jquery dialog box without getting the text from a div, but rather from text in a javascript variable?

3
  • 1
    How about setting the text of the div to your var when you open the dialog? Commented Jul 16, 2013 at 2:38
  • Looks like this example, only just declare your Javascript variable and insert it into the JQuery method. Remember, JQuery is Javascript. stackoverflow.com/questions/366696/jquery-dialog-box Commented Jul 16, 2013 at 2:39
  • Why would you want to do that? You could trigger any event and store something to a var. Commented Jul 16, 2013 at 2:51

1 Answer 1

3

You can create and append a <div> element from a javascript variable and then turn it into a dialog using something like this:

var dialog =  "<div id=dialog <h1>Some text</h1></div>";
$('body').append(dialog);
$('#dialog').dialog();  

FIDDLE

var dialog =  "<div id=dialog <h1>Some text</h1></div>";
$('body').append(dialog);
$('#dialog').dialog();
<head>
  <title>jQuery UI Dialog - Default functionality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script> 
<link href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>

  
</head>
<body>

</body>

You can automate this with a function:

var createDialog = function(text , title) {
    var dialog =  "<div id=dialog <h1>" + text + "</h1></div>";
    $('body').append(dialog);
    $('#dialog').prop('title' , title);
    $('#dialog').dialog();
}

Just call the function passing your variables in as arguments and you get your dialog.

FIDDLE

// initialize title and body variables
var someVariable = "Some text";
var someTitle = "My title";

var createDialog = function(text , title) {
    //create dialog <div> shell
    var dialog =  "<div id=dialog <h1>" + text + "</h1></div>";
    
    // create the dialog <div>
    $('body').append(dialog);
    
    // update the <div>'s title
    $('#dialog').prop('title' , title);
    
    //create the dialog
    $('#dialog').dialog();
}

createDialog(someVariable , someTitle);
<head>
  <title>jQuery UI Dialog - Default functionality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
 <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
</head>
<body>

</body>

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

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.