jquery dialog submit form in iframe - jquery-ui

I'm using jquery to open a dialog containing an iframe (don't ask!). I want to submit the form in the iframe on closing the dialog but it isn't working.
I'm probably making a simple error (I'm quite new to jquery) but this problem has been driving me round the bend.
Here is my code:
$(function() {
$( "#iframe" ).dialog({
modal: true,
autoOpen: false,
height: 500,
width: 700,
buttons: {
"Save and close": function() {
$( "#iframe").contents().find("#contentform").submit();
$( "#iframe" ).dialog( "close" );
},
Cancel: function() {
$( "#iframe" ).dialog( "close" );
}
}
});
$( "#openProfile" ).click(function() {
$( "#iframe" ).dialog( "open" );
$('#iframe').attr('src','file.asp');
return false;
});
});
However, if I do this instead of submitting the form:
"Save and close": function() {
var myformvalue = $( "#iframe").contents().find("#formfield").val();
alert(myformvalue);
$( "#iframe" ).dialog( "close" );
}
...it returns the correct value so I know it is recognising my form and its values.
Thanks in advance for your help.

I think you are running up against the jQuery V1.10(/9?) issue where they changed it so that the dialog container is moved out of the DOM, so you don't end up getting postbacks from the page in the iFrame. Reference this: https://stackoverflow.com/a/24663205/3334255 and An ASP.NET button click event is not firing

Related

Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'

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

Pass a variable to JQuery UI dialog

I am deleting a record using PHP. I want to use a JQuery UI dialog to confirm the action, but I dont know how to pass a variable (my RecordID) to the redirect URL function, or allow the URL to access window.location.href.
$("#confirm" ).dialog({
resizable: false,
autoOpen: false,
modal: true,
buttons: {
'OK': function() {
window.location.href = 'url and myvar??';
$( this ).dialog( "close" );
},
'Cancel': function() {
$( this ).dialog( "close" );
}
}
});
$("#delete").click(function() {
$("#confirm").dialog( "open" ).html ( "Are U Sure?" );
return false;
});
HTML
<a href='index.php?recordid=$row[recordid]' id='delete'>DELETE</a>
Is there a good way to do this?
You can try using the .data() method to store data for you. Take a look at this answer
Passing data to a jQuery UI Dialog
For example to pass a variable, you can store it using the data function, before opening the dialog
$("#dialog_div")
.data('param_1', 'whateverdata')
.dialog("open");
Then you can get this back by:
var my_data = $("#dialog_div").data('param_1')
You want to change the configuration of the dialog on click (in this case, the behaviour of the Ok button). For that your have many solutions all of them ugly (imo). I would advice generating a dialog on the fly, and destroying it once it has been used, something like this:
$("#delete").click(function(ev) {
ev.preventDefault(); // preventDefault should suffice, no return false
var href = $(this).attr("href");
var dialog = $("<div>Are you sure?</div>");
$(dialog).dialog({
resizable: false,
autoOpen: true,
modal: true,
buttons: {
'OK': function() {
window.location = href;
$( this ).dialog( "close" );
},
'Cancel': function() {
$( this ).dialog( "close" );
}
},
close: {
$( this ).remove();
}
});
});
Or even better, encapsulate the confirm dialog into a function so that you can reuse it, like so:
function confirmDialog(msg) {
var dialog = $("<div>"+msg+"</div>");
var def = $.Deferred();
$(dialog).dialog({
resizable: false,
autoOpen: true,
modal: true,
buttons: {
'OK': function() {
def.resolve();
$( this ).dialog( "close" );
},
'Cancel': function() {
def.reject();
$( this ).dialog( "close" );
}
},
close: {
$( this ).remove();
}
});
return def.promise();
}
And then use it like so
confirmDialog("are your sure?").done(function() {
window.location = $(this).attr("href");
}).fail(function() {
// cry a little
});
You may have to check if the deferred object has been rejected or resolved before you close the dialog, to ensure the confirm rejects on close (and not just on pressing the 'Cancel' button). This can be done with a def.state() === "pending" conditional.
For more information on jquery deferred: http://api.jquery.com/category/deferred-object/
Deleting actions probably shouldn't be done using a GET, but if you wanted to do it that way I would recommend using the $.data in jQuery so each link had a data-record-id attribute. Then on click of one of the links, it pops up the dialog and when confirmed it adds that to the URL, and redirects.
Example:
$(function(){
$(".deleteLink").click(function(){
var id = $(this).data("record-id");
var myHref = $(this).attr('href');
$("#confirmDialog").dialog({
buttons:{
"Yes": function()
{
window.location.href = myHref + id;
}
}
});
});
});
<a class="deleteLink" data-record-id="1">Delete</a>
...
<div id="confirmDialog">
<p>Are you sure?</p>
</div>
HTML
<a data-title="Title" data-content="content" data-mydata="1" class="confirmation-dialog" href="#">Link</a>
JS
$('.confirmation-dialog').confirm({
buttons: {
Yes: function(){
console.log(this.$target.attr('data-mydata'));
No: function(){
}
}
});

Dialog in jquery is not working

I have written this code but it is not working.
$("#dialog-confirm").text("Do you want to submit the new vendor for approval?");
$( "#dialog-confirm" ).dialog({
resizable: true,
title:"Submit",
height:170,
zIndex:99999,
modal: true,
position: "center",
buttons: {
"Yes": function() {
$( this ).dialog( "close" );
return false;
},
"No": function() {
$( this ).dialog( "close" );
return false;
}
}
});
Please help.
I must use answer,as i dont have permission to comment yet. Your code doesnt look bad,and if you put it to jsfiddle it actually works (at least in chrome). Are you missing reference to jquery or jquery-ui library?

jQuery UI delete confirm call back function

I wang to use jQuery UI to implement a delete confirmation. I want to use a link to trigger the dialog. Here's my code:
delete
<script type="text/javascript">
$(function(){
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
autoOpen: false,
buttons: {
"Okay": function() {
$( this ).dialog( "close" );
return true;
},
Cancel: function() {
$( this ).dialog( "close" );
return false;
}
}
});
$( ".delete" ).click(function() {
$( "#dialog-confirm" ).dialog( "open" );
return false;
});
});
</script>
What I want to do is, when the user click on cancel, it does nothing. But when the user click on okay, then it will continue to go to www.google.com.
However, no matter okay or cancel I click, it remains nothing happens. Any idea?
Even if you do this:
$("a.delete").trigger("click");
You still won't get to google, because after it calls handlers, jQuery triggers an event on the object. It only calls native handlers for click events if the element is not a link.
You could do something like this:
window.location.href = $("a.delete").attr("href");
Or, you could have a hidden link on the page, like this:
delete
Then, on the click of the "Open" button, you could do this:
$("a.hiddenDelete").trigger("click");

How can WYMeditor and Jquery UI Dialog work together

In my modal dialog, when I'm done editing with WYMeditor, I want to click the OK button and have it update the Textarea. I don't see any example how to use this with a Jquery UI modal dialog, so I'm kind of stuck.
As long as I know the instance of it, I can set it as such.
$('#editContent').wymeditor();
var wym = WYMeditor.INSTANCES[0];
$('#dialog').dialog({
buttons: {
Ok: function() {
wym.update();
var newContent = $('#editContent').val();
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
//$( this ).dialog( "close" );
}
});
Seems to work... finally.

Resources