When I call the function, the dialog doesn’t work.
If I move the dialog construction into the showtimes_list function, everything works fine.
I thought that variables declared outside a function were global in context?
var dialog_list = $("<div></div>").dialog({
autoOpen: false,
modal: true,
height: 300, width: 720,
});
function showtimes_list(sid){
dialog_list.html("");
$.get("ajax_showtimes.php?sid="+sid, function(data){
dialog_list.html(data);
}
);
dialog_list.dialog("open");
}
—Edit—
This is being called from an onClick to showtimes_list.
—Edit—
This is working:
function showtimes_list(sid){
$("#stl").dialog("open");
$("#stl").html("");
$.get("ajax_showtimes.php?sid="+sid, function(data){
$("#stl").html(data);
}
);
}
$(function(){
$('<div id="stl"></div>').appendTo(document.body).dialog({
autoOpen: false,
modal: true,
height: 300, width: 720,
});
});
Instead of creating a new empty div, add the div to the document, e.g.:
Then, your script will become
Then, when you want to change the HTML of that element, instead of changing the dialog itself, you’d want to change the dialogContent element:
If you don’t want these empty divs in your HTML structure, you should add them to the body dynamically and use classes instead of ids.
Edit: to answer your question as to why it doesn’t work when the dialog_list is outside the function, I’d imagine it has something to do with the generated html.
When you call .dialog(), jQuery generates the following HTML:
When this is outside of your function, it is called whenever it is encountered in the script… Then, in your function, you change that generated HTML. It’s been a while since I dynamically updated dialog content, but I ran into the same problem, and the generated HTML was the culprit. I think my solution was to, instead of the nested divs in my original answer, I created the dialog as you did (outside the function), and inside the function, you change the html of the
ui-dialog-contentFor instance: