I have the following jQuery-UI dialog with 2 buttons:
$("#dialog").dialog({
autoOpen: false,
width: 400,
buttons: [
{
class: "btnOK",
click: function () {...
}
},
{
class: "btnClose",
click: function () {
$(this).dialog("close");
}
}
]
});
I can disable btnOK with:
$('.btnOK').attr("disabled", true).addClass("ui-state-disabled");
OR $('.btnOK').button("disabled");
OR $('.btnOK').button( "option", "disabled",true);
However this doen't block click on the button.
How I can disable click?
Thanks.
The working ways are:
$('.btnOK').button("disable");
OR $('.btnOK').button( "option", "disabled",true);
OR $('.btnOK').attr("disabled", true).addClass("ui-state-disabled");
I just have not cleared the history in my browser, so I didn't see the changes.
Related
I'm using Jquery UI dialog box and I want to add a button. Can I use .html() as the function in the dialog?
$(".selector").dialog({
buttons: [{
text: "Ok",
click: function () {
$(this).dialog("close");
},
{
text: "View Details",
click: function () {
$(this).html('Some HTML');
}
}]
});
Use the option method to change the dialog's buttons on the fly. First use it with one argument to get the old buttons, modify that array, then use it with two arguments to change the buttons.
$( ".selector" ).dialog({
buttons: [
{
text: "Ok",
click: function() {
$( this ).dialog( "close" );
}
},
{
text: "View Details",
click:function(){
var buttons = $(this).dialog("option", "buttons");
buttons.push({
text: "New button",
click: function() {
alert("New button clicked");
}
});
$( this ).dialog("option", "buttons", buttons);
}
}
]
});
So I have these two dialog() calls:
$('#dialog').dialog();
$('#dialog').dialog('option', 'width', 500);
Is there a way to consolidate that down into one? I tried this without success:
$('#dialog').dialog({
option: [{ width: 500 }]
});
Here is an example from a project I am working on.
$( "#username_availability_dialog" ).dialog({
closeOnEscape: false,
resizable: false,
height:120,
width: "48%",
modal: true,
buttons: {
"OK": function() {
$( this ).dialog( "close" );
$('#dealName').val('');
$("#dealsForm [name='dealName']").focus();
return false;
},
Cancel: function() {
$( this ).dialog( "close" );
window.location.reload();
}
}
});
You don't need the square brackets or the option method when initializing the dialog widget using an object to store the options.
$('#dialog').dialog({
width: 500
});
The format you were using to set options actually REQUIRES that the widget be initialized beforehand.
jQuery-UI Dialog "width" option
Just a little preface... I am fairly new to jQuery so if something looks wrong or redundant please feel free to offer any helpful suggestions.
Now for the issue. I have 2 modals that are initiated from 2 separate links on the page.
The first modal was fairly problem free. It is a simple form that posts back to the same page. If you are wondering what the items in the "close:" section are, they are form fields that I want to clear the values on when the dialog is closed.
Once I added the second one I have had problems. This modal calls a coldfusion page into a modal to display pictures. The problems occur after opening up the second one. I can not close the second modal from the "close" button. I get the following error.
Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'
I have to close it from the "x" in the upper right hand corner of the modal. After I close it, I get an error when trying to open up the first on.
Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'
Here is the code for it.
$(document).ready(function() {
$(".dig").click(function() {
//based on which we click, get the current values
var cItemName = $("#checklistItemName").attr( "title");
var c2id = $("#check2id").attr( "title");
$("#ItemName").html(cItemName);
$("#ItemID").html(c2id);
$("#objCheckItemName").val(cItemName);
$("#objCheck2ID").val(c2id);
console.log(cItemName);
console.log(c2id);
});
$( "#image-form" ).dialog({
autoOpen: false,
height: 450,
width: 650,
modal: true,
buttons: {
"Submit": function() {
$('#mForm').submit();
return true;
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
$('#defaultSectionName')
.val('');
$('#defaultSectionDesc_hidden')
.val('');
$('#Photo')
.val('');
$('#objCheck2ID')
.val('');
$('#Check21')
.val('');
},
zIndex: 500
});
The next piece of code is where I believe the problems are occurring.
$( "#image_trigger" )
.click(function() {
$( "#image-form" ).dialog( "open" );
});
var dlg=$('#register').dialog({
title: 'Case Pictures',
resizable: true,
autoOpen:false,
modal: true,
hide: 'fade',
width:650,
height:450,
buttons: {
close: function() {
$( this ).dialog( "close" );
}
},
zIndex: 500
});
$('#reg_link').click(function(e) {
e.preventDefault();
var linkurl=('assets/includes/modalPictures.cfm' + '?'
+ 'id=' + $("#objCheck2ID").val()
);
dlg.load(linkurl, function(){
dlg.dialog('open');
});
});
jQuery UI: 1.10.1
jQuery: 1.9.1
Server Side: Coldfusion
The HTML is pretty extensive. If you need to see any part of it please let me know. Thanks for your help!
Upper case?
Close: function() {
You have to initialize your jquery dialog before calling the open function.
<script type="text/javascript">
$(document).ready(function () {
initializeDialog();
});
</script>
And change your js file to have the dialog code in initializeDialog() function.
function initializeDialog(){
$("#your-dialog-id").dialog({
autoOpen: false,
buttons: {
"Cancel": function() {
$(this).dialog("close");
}
},
modal: true,
resizable: false,
width: 600px,
height: 400px
});
}
Thanks #bpjoshi
As I said in a past post, I'm a JavaScript newbie, and now learned some JQuery.
Briefing:
I need to create a dialog box that triggers when someone clicks on Cancel, and has (as text or value)"Are you sure you want to exit?", with two buttons: "Yes, I want to leave" and "No, I want to stay on this page". The redirect part from the "I want to leave" button will figure it out later.
Problem: I want to make the buttons to be autoSizable with the width option (in other words, width:auto and height: (number)px, but I can't figure it out :/
This is my code ->
$(document).ready (function(){
var $dialog = $('<div></div>');
$(".selector").dialog({ modal:true });
var $popUp = $('#btnCancel').one('click', function () {
$dialog
.html('All changes will not be saved')
.dialog({
title: 'Are you sure you want to exit?',
resizable: false,
modal: true,
closeOnEscape: true,
buttons: {
"Yes, I want to leave": function() {
$( this ).dialog( "close" );
},
"No, I want to stay on this page": function() {
$( this ).dialog( "close" );
}
}
});
});
$popUp.click(function () {
$dialog.dialog('open');
return false;
});
});
I tried changing the buttons part for this piece of code ->
buttons: [
{ text: "Exit", 'class':'exit-button', resizable: true, click: function () { $(this).dialog("close"); } },
{ text: "Go back", 'class': 'go-back-button', resizable: true, click: function () { $(this).dialog("close"); } }
]
But in this last case, it creates two buttons with the value of "0" and "1", and have no onclick event.
What should I do?
If you give the buttons an id when you create them you can style them with CSS, including giving them a height and width.
Example - http://jsfiddle.net/tj_vantoll/ckhG6/2/
When I create a modal box everything works perfectly. However when I want to open another modal box by clicking the OK button from the first modal box this will open up the next modal box but the CSS for applying the image background with recude opactiy does not seem to be used once this happens.
Has anyone ever tried this and if so is there some way I can make this style still apply when the second modal box opens and the first one closes.
Thanks
Rory
$('#dialog1').dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
$('#dialog2').dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
$(this).dialog("close");
$('#dialog2').dialog('open');
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
I managed to fix this by simply adding the overlay to the <body> via JQuery