Jquery UI Dialog open on if condition - javascript

I am creating a little game in jquery, where I want to show the user a little box, if they won the game.
The condition here is (correctcards ==10)
I tried to call dialog on it but it wont work.
Can anyone tell me why?
(I also aready tried to create a new div, when correctcards turn 10 and call dialog on it then, but it doesnt work as well)
if (correctCards == 10) {
endTime = new Date().getTime();
time();
console.log('Spiel endet');
$('#myDialog2').dialog({
autoOpen: true,
modal: true,
resizable: false,
draggable: false,
buttons: {
'Next Level': function() {
$(this).dialog('close');
},
'Close': function() {
$(this).dialog('close');
},
}
})
}
<div id="myDialog2">You won</div>

You need to create the dialog outside of your if.
$('#myDialog2').dialog({
autoOpen: false,
modal: true,
resizable: false,
draggable: false,
buttons: {
'Next Level': function() {
$(this).dialog('close');
},
'Close': function() {
$(this).dialog('close');
},
}
});
var correctCards = 10;
if (correctCards == 10) {
console.log('Spiel endet');
$('#myDialog2').dialog('open');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
<div id="myDialog2" style>You won</div>

Related

Function cannot be called in $(document).ready(function() {

The questionis more of a debuggin/syntax error rather approach .
I have a function(modal confirmation) defined in an external js file which returns a value as such :
function confirmation(question) {
var defer = $.Deferred();
$('<div></div>').html(question).dialog({
autoOpen: true,
modal: true,
title: 'Confirmation',
buttons: {
"Delete All Items": function() {
defer.resolve("true"); //this text 'true' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
},
"Cancel": function() {
defer.resolve("false"); //this text 'false' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
}
},
close: function() {
//$(this).remove();
$(this).dialog('destroy').remove()
}
});
}
Now when I try to call the function inside the $(document).ready(function() {; I get an Uncaught Reference Error.
All the necessary files have been included in calling script. I would like to understand why is this and how i can solve the issue?
Except for the missing curly-brace at the end, and assuming your "necessary files" include jquery-ui, there doesn't seem to be anything wrong with your function. See this jsfiddle, which does not generate any error.
Perhaps the problem is elsewhere in your code? It may help if you can post a Minimal, Complete, and Verifiable example.
References:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.theme.css" />
Script:
$(document).ready(function() {
confirmation("What's all this, then?");
});
function confirmation(question) {
var defer = $.Deferred();
$('<div></div>').html(question).dialog({
autoOpen: true,
modal: true,
title: 'Confirmation',
buttons: {
"Delete All Items": function() {
defer.resolve("true"); //this text 'true' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
},
"Cancel": function() {
defer.resolve("false"); //this text 'false' can be anything. But for this usage, it should be true or false.
$(this).dialog("close");
}
},
close: function() {
//$(this).remove();
$(this).dialog('destroy').remove()
}
});
}

AlloyUI modal reuse modal

I'm working on a AlloyUI modal window that is present in many pages of my application. The modal structure is actually the same, the only thing that changes is the bodyContent text for each page. I'm trying to reuse the AlloyUI modal script, only updating the bodyContent parameter rather than create 20 modal scripts for each page, but it's script nightmare for me as I have not found any code I can look at. I created a jsfiddle as an example and down below is the script I've been working. I'd appreciate your help.
http://jsfiddle.net/x9t3q0bs/
YUI().use('aui-modal', function(Y) {
var helpModConfIdent = Y.one('#showHelpPageConfirmIdentification'),
helpModQuestions = Y.one('#showHelpPageQuestions'),
helpPageConfirmIdentRetCust = Y.one('#showHelpPageConfirmIdentRetCust')
var modal = new Y.Modal({
bodyContent: "<p>Here will show help modal1.</p>",
centered: true,
destroyOnHide: false,
headerContent: '<h3>Help info</h3>',
modal: true,
render: '#modal',
visible: false,
width: 800,
toolbars: {
}
});
modal.addToolbar([{
label: 'Close',
cssClass: 'btn-primary-content',
on: {
click: function() {
modal.hide();
}
}
}]);
modal2 = new Y.Modal(
{
bodyContent: "<p>Here will show help modal2.</p>",
centered: true,
destroyOnHide: false,
headerContent: '<h3>Help info</h3>',
modal: true,
render: '#modal',
visible: false,
width: 800,
toolbars: {
}
}
);
if (helpModConfIdent) {
helpModConfIdent.on('click', function (e) {
modal.show();
});
} else if (helpModQuestions) {
helpModQuestions.on('click', function (e) {
modal2.show();
});
}
});
Thanks
The bodyContent is one of the attributes that is available to be set if you have access to the modal instance. Otherwise, you can always manipulate the html within the template that has been rendered.
YUI().use('aui-modal', function(Y) {
var modal = new Y.Modal({
bodyContent: "<p>Default implementation</p>",
centered: false,
destroyOnHide: false,
headerContent: '<h3>Help info</h3>',
modal: true,
render: '#modal',
visible: false,
width: 250
});
Y.one('#modalInstance').on('click', function(){
modal.set('bodyContent', "<p>Something loaded using the orginal modal instance</p>")
modal.show()
})
Y.one('#nodeInstance').on('click', function (e) {
Y.one('#modal .modal-content .modal-body').setHTML('<p>Set using the node instance</p>')
modal.show()
})
});
<script src="http://cdn.alloyui.com/3.0.0/aui/aui-min.js"></script>
<link href="http://cdn.alloyui.com/3.0.0/aui-css/css/bootstrap.min.css" rel="stylesheet"></link>
<div id='modalInstance'>Modal Instance</div>
<br/>
<div id='nodeInstance'>Node Instance</div>
<div class="yui3-skin-sam">
<div id="modal"></div>
</div>

wait till jquery dialog box returns

I need to open a popup window on clicking a button and used jquery dialog for this.
$(document).ready(function(){
$("#dialog-form").dialog({
autoOpen : false,
height : 300,
width : 350,
modal : true,
buttons : {
"Add" : function() {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
},
Cancel : function() {
$(this).dialog("close");
}
},
close : function() {
$("#textArea").val("");
}
});
});
function openWindow(){
$("#dialog-form").dialog("open");
statement1;
statement2;
}
<button id="add" onclick="openWindow()">Add</button>
problem over here is when i click the button dialog box is opened, but before even i enter some text in the dialog box statement1 and statement2 are getting executed and then focus is coming to the dialog box.
How can i make the statement1 and statement2 to execute only after dialog box returns?
I don't want to add the statement1 and statement2 to the "Add" function. The reason for not adding the statements in the "Add" function is because i have multiple buttons and each of these should first open the dialog box and then will execute different set of statements.
Easy fix would be to use the close callback:
$(document).ready(function () {
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
$("#textArea").val("");
//statement1 -- won't fire until dialog is closed
//statement2 -- won't fire until dialog is closed
}
});
});
function openWindow() {
$("#dialog-form").dialog("open");
}
Another thing to consider would be $.Deferred
I have an example for you:
$(".selector").click(function () {
var dialog = $('<div title="Title"></div>').dialog({
open: function (event, ui) {
$.ajax({
url: 'www.google.com.br',
cache: false,
success: function (html) {
$(dialog).html(html);
},
error: function () {
$(dialog).remove();
alert("Some Error MSG");
}
});
},
close: function () {
$(dialog).remove();
},
resizable: false,
width: 500,
modal: true
});
});
In this case, the dialog is receiving the HTML result, only after it opens.
This can be achieved by the following way:-
$('#mydialog').dialog("open");
$('#mydialog').load('serverURL',server_data_variable, function() {
myfunction();
});
This will execute the function once the dialog loading is done.It will register the callback to be executed post dialog done.The variable server_data_variable is optional and is supposed to be used only if user wants to send some data otherwise it can be skipped as.well.
Solution 1: (same as solution from Aaron Blenkush)
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
//statement1 -- will fire only if "add" button is clicked
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
$("#textArea").val("");
//statement1 -- will fire after dialog is closed
}
});
Solution 2 is to make a promise:
const dialogPromise = new Promise(function (resolve, reject) {
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
resolve(true);
},
Cancel: function () {
$(this).dialog("close");
resolve(false);
}
},
close: function () {
$("#textArea").val("");
}
});
});
const addClicked = await dialogPromise;
Call callback function after "open" clause in the dialog setup.
modal: true,
resizable: false,
resize: 'auto',
close: dialogCloseFunction,
**open: function(){if(itemid) {showDailogSides(itemid);}** if(!siteParams[3]) {$(".detailSideClass").hide(); $(".addToChartinDialog").hide();}},
//hide: {effect: "fadeOut", duration: 5000}
show: { effect: "fade", duration: 1000 } //drop blind fade fold slide clip

Jquery dialog working in IE perfectly but not in FF and Chrome

here is my simple jquery which is working fine in IE but not working in FF and Chrome
function publishRedirects() {
if (window.confirm("Are you sure want to publish the redirects?")) {
callJquery();
}
}
function callJquery() {
$('#btnPublish').click(function () {
$("#dialog").attr('title', 'Publishing').text('Publishing Please Wait').append("<img src='../images/loading11.gif' id='myNewImage' />").dialog({
draggable: false,
resizable: false,
modal: true,
closeOnEscape: false,
open: function (event, ui) {
$("a.ui-dialog-titlebar-close").remove();
}
});
});
}
This is my HTML:
<body> <div id="dialog"></div> <input name="btnPublish" class="button1" type="button" id="btnPublish" value="Publish" onClick="publishRedirects()"> <script type="text/javascript" src="../script/jquery.js"></script> <script type="text/javascript" src="../script/jquery-ui.js"></script> </body>
I think it is because you are assigning a click event to btnPublish instead of executing the function. if publishRedirects() is called on click of a button and you want the code in callJquery to execute after the user confirms the dialog box, it should look something like this: (notice the removed attach to click event)
function callJquery(){
var obj= $("#dialog");
obj.attr('title','Publishing');
obj.text('Publishing Please Wait');
obj.append("<img src='../images/loading11.gif' id='myNewImage' />");
obj.dialog({
draggable: false,
resizable: false,
modal: true,
closeOnEscape: false,
open: function(event, ui) {
$("a.ui-dialog-titlebar-close").remove();
}
});
}

jquery dialog not opening/closing

i have code like this:
The dialog does not open when i use this.
else if (json.score == -3) {
$("#dialog-unauthenticated").dialog('open');
}
but does when i use this! I have it initialized with autopen false above too.
else if (json.score == -3) {
$("#dialog-unauthenticated").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
}
}
});
}
what is wrong?
close does not work either.
initialized with:
$("#dialog-unauthenticated").dialog({
autoOpen: false,
resizable: false,
height: 140,
modal: true,
buttons: {
"OK": function () {
$(this).dialog("close");
}
}
});
Not sure if this helps, but im invoking this in response to a jquery post.
you need to initiate the dialog first.
then you can do actions after.
so e.g.
$('<div id="dialog" />')
.dialog({
modal:true,
buttons:{
cancel:function(){
$(this).dialog('close');
}
}
});//init dialog
$('#open').click(function (){
$('#dialog').dialog('open');
});
$('#close').click(function (){
$('#dialog').dialog('close');
});
As you are trying to open the dialog when its not initiated. therefore the dialog does not exist so it cnt be opened

Categories