I am wondering whether is there a way to consolidate the script in the head of the document so when you have multiple dialogs, you don't have this huge chunk of your header taken up? For example, if you had say 10+ dialog boxes, you would have the following entered 10 times. It's super redundant. Is there a way to just say #dialog1-20 and have it all in one thing?
javascript:
// Dialog 1
$('#dialog1').dialog({
stack: false,
autoOpen: false,
width: 500,
buttons: {
"OK": function() {
(...some action...)
},
"Close": function() {
$(this).dialog("close");
}
}
});
// Dialog Link 1
$('#id_1').click(function(){
$('#dialog1').dialog('open');
return false;
});
// Dialog 2
$('#dialog2').dialog({
stack: false,
autoOpen: false,
width: 500,
buttons: {
"OK": function() {
(...some action...)
},
"Close": function() {
$(this).dialog("close");
}
}
});
// Dialog Link 2
$('#id_2').click(function(){
$('#dialog2').dialog('open');
return false;
});
HTML:
<div id='dialog1' title='title1' style='display: none;'>(...)</div>
<div id='dialog2' title='title2' style='display: none;'>(...)</div>
EDIT:
As suggested (thanks to Pietu1998, Ed and Osama.070032) I changed to the class style but I am still missing something. In addition I need to pass variables across the elements which is something I don't know how to do. My code now looks like below - you can see my issues in the comments below:
<script type='text/javascript'>
$(function(){
for (i = 0; i <= <? echo $items_count; ?>; i++) {
$('#dialog_name_' + i).dialog({
autoOpen: false,
width: 600,
modal: true,
buttons: {
'Cancel': function() {
$(this).dialog('close');
},
'OK': function() {
$.ajax({
url: 'some_file.php',
type: 'POST',
data: 'item_id=' + i,// here I need to pass variable
});
$(this).dialog('close');
}
}
});
$('#link_dialog_name_' + i).click(function(){
$('#dialog_name_' + i).dialog('open');
return false;
});
}// for end
});
</script>
<?
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {// I am wondering if I can get rid of this loop and pass variable to see item's name below
?>
<a href='#' id='link_dialog_name_<? echo $line["item_id"]; ?>'></a>
<div id='dialog_name_<? echo $line["item_id"]; ?>' title='Name' style='display: none;'>
<?
if ($line["name"]=="") {
echo "Number 1 does not have any name.";
} else {
echo "The name of number 1 is ".$line["name"];
}
}// while end
?>