In my django template, I have a block of code which looks as follow:
{% block b %}
{ % for foo in foos %}
{ % if foo.some_variable % }
<form method="LINK" action="{% url "some_view" foo.id %}" id="button">
<input type="submit" value="Button"/></form>
{ % else % }
<form method="LINK" action="{% url "some_view1" foo.id %}" id="button">
<input type="submit" value="Button1"/></form>
<form method="LINK" action="{% url "some_view2" foo.id %}" id="button">
<input type="submit" value="Button2"/></form>
{ % endif %}
<br>
{ % endfor %}
{% endblock % }
I need to make confirmation popup for all of those buttons. So I tried to add
onclick = "return confirm("are u sure?")"
And it works, but it's ugly. I want to add custom buttons names, header and so on.
So I read that this is not possible in clear javascript and I should use jquery.
I tried something like this, but since I don't know jquery neither js it doesn't work:
<script>
function confirmAction(){
var confirmed ;
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Yes": function() {
confirmed = true;
$( this ).dialog( "close" );
},
"No": function() {
confirmed = false;
$( this ).dialog( "close" );
}
}
});
return confirmed
}
</script>
<div id="dialog-confirm" title="Are you sure??">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
Are you sure you want to do this?</p>
</div>
When I click on button, popup appears, but it suddenly disappears and I'm redirected to a view anyway.
Could you try to help me?
Best regards,
confirmed is always false because javascript is asynchrone.
You need to use some closure. Something like this should do the trick:
$('input:submit').click(function() {
// get form associated with the button
var form = $(this).parents('form')
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Yes": function() {
$( this ).dialog( "close" );
// maybe set an hidden field to keep button value if needed
form.submit()
},
"No": function() {
$( this ).dialog( "close" );
}
}
});
return false;
}
Another problem is that I also have other buttons designed this way (form, submit) on my site.
But I've got it work with this piece of code:
<script>
$('.popup_button').click(function() {
// get form associated with the button
var form = $(this)
$("#dialog-confirm").dialog({
resizable: false,
height:200,
modal: true,
buttons: {
"Yes": function() {
$( this ).dialog( "close" );
form.submit()
},
"No": function() {
$( this ).dialog( "close" );
}
}
});
return false;
});
</script>
<div id="dialog-confirm" title="Confirmation" style='display: none'>
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
Are you sure?</p>
</div>
So confirmation popup appears only for those buttons, which have class 'popup_button'.
The only thing I'm missing is how t get custom confirmation message for each button.
When I substitute
$("#dialog-confirm").dialog
with
$(".new_button_class").dialog
and then add class='new_button_class' and 'title' attributes to my button forms, strange things happen. What is more, I don't now how to set dialog message this way.
adding
$(".new_button_class", form).dialog
instead causes popups not to appear at all.
How to fix it?
Related
I'm just starting out with jquery. Already learned some things and like it, but I have been struggling with the following issue for a few days.
I copied the "dialog-confirm"-function from https://jqueryui.com/. I placed this script between the tags on my index.php page.
<script type = "text/javascript">
$(document).ready(function(){
$(function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
$(window).resize(function() {
$('#scrollpage').height($(window).height() - 250);
});
$(window).trigger('resize');
$('.container').on('click', '.mainmenu', function(event){
event.preventDefault();
var url = $(this).attr('href');
$.get(url, function(data) {
//alert(data);
$("#div1").load(url);
});
$( this ).parent().addClass('current_page_item');
$( this ).parent().siblings().removeClass('current_page_item');
});
$('.container').on('click', '.rapport', function(event){
event.preventDefault();
//$(".dialog-confirm").dialog( "open" );
var url = $(this).attr('href');
$.get(url, function(data) {
//alert(data);
$("#div1").load(url);
});
});
});
</script>
If i place the matching div in the same index.php page. It works fine, the div pops up.
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Blablabla</p>
</div>
However when i place the div in a page which is loaded by ajax in the div1, then I cant get it to work.
<div class="scrollpage" id="scrollpage">
<div class="container" class="page" id="div1">
</div>
</div>
Can anyone explain to me why this is, and how I can fix this?
What's happening is that your $(document).ready() function executes as soon as the DOM is loaded. This DOM contains just what it is in the html file. At that time, there's no div with an id equal to 'dialog-confirm'. Loading pieces of HTML with ajax doesn't trigger a DOMReady event. What you've got to do is to call the .dialog() jQuery function AFTER you've loaded the div with Ajax:
$("#div1").load(url, function() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
Here is what happens in your program(loading div with ajax):
First, your script initiates dialog window container by looking an element with id dialog-confirm.
Since, you don't have an element with that id yet, dialog container cannot be prepared.
There are two ways you can make it work:
Call dialog() after ajax requests,
Place div statically on page and change content with ajax request.
Solutions:
1- Use the code below instead of $("#div1").load(url);
$("#div1").load(url, function(){
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
2- Place divs statically on your page:
<div class="scrollpage" id="scrollpage">
<div class="container" class="page" id="div1">
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Blablabla</p>
</div>
</div>
</div>
Then load just <p>... with $("#dialog-confirm").load(data); instead of $("#div1").load(url);.
Your jquery is executed only once after your page is loaded. Then is initiating everything needed for the tool!
This means every .container is being 'transformed'.
If you later add a new div.container it is not 'transformed' yet. You have to execute the jquery again after your div is appended!
I want to use multiple images to open the same jQuery dialog box. I have noticed that this only works for the first image, is there any way to make the second one work as well without giving it a unique id? There will be 100+ of these images so I don't want to create a new function for each image
$(function() {
$("#dialog").dialog({
autoOpen: false},{ buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ]});
$("#IMG").on("click", function() {
$("#dialog").dialog("open");
});
});
<div id="dialog" title="Dialog box">Test</div>
<input type="image" id="IMG" src="img/image.gif">
<input type="image" id="IMG" src="img/image.gif">
Specify a class for each image, say "img". Then declare a handler like this:
$(".img").on("click", function() {
$("#dialog").dialog("open");
});
IDs have to be unqiue. Use a class instead:
$(function() {
$("#dialog").dialog({
autoOpen: false},{ buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ]});
$(".open-img").on("click", function() {
$("#dialog").dialog("open");
});
});
HTML:
<div id="dialog" title="Dialog box">Test</div>
<input type="image" class="open-img" src="img/image.gif">
<input type="image" class="open-img" src="img/image.gif">
I have a button in a my webpage; my expectation is when I click the button, it prompts a dialog window; in the dialog window, it has yes/no button; if I click yes, it opens a new page (gereated by php); if I click no, I stay on the old page. How can I do that?
<input type="button" name="terminate" value="terminate" id="terminate" title="Terminate" onclick="terminateIt();"/>
There are innumerable ways to do this.
Probably the most common way would be to use jQuery UI's Dialog.
They have an example in their demo of a dialog with two buttons. Here is a snipped from that demo:
HTML:
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Your question goes here. Are you sure?</p>
</div>
JS:
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
Yes: function() {
window.open(....);
$( this ).dialog( "close" );
},
No: function() {
$( this ).dialog( "close" );
}
}
});
Check out the whole jQuery UI library. Tons of great stuff making it easy to do some basic things.
function terminateIt(){
var r = confirm("Go to new page?");
if( r == true ){
// redirect stuff here
} else {
// non redirect stuff here
}
}
can someone please tell me why is the jquery confirm modal doesn't work on my app?
here's my code
<div id="dialog-confirm" title="Empty the recycle bin?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
here's the js code
if(userid == ""){
$( "#dialog:ui-dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
buttons: {
"Delete all items": function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
}
there's no pop-up that come out, but when i tried the simple modal alert, it works
i have this in my header
jquery-ui.min.js
I solved my problem by including the dependency js files ROFL
Can I write a custom confirm box in JavaScript that, instead of the default OK and CANCEL button, shows a SAVE and DELETE button?
Use the jQuery UI Dialog Box for this.
You can do something like this:
JS:
$(function() {
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Do it": function() {
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
});
<link href="https://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/>
<link href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css" rel="stylesheet" />
<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.js"></script>
<div id="dialog-confirm" title="Is it treason, then?">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Am I the Senate?</p>
</div>
Not with the native JavaScript confirm box. You could use one of the many JavaScript UI components that emulate modal dialogs to do this instead.
Here's an example: https://jqueryui.com/dialog/#modal-confirmation
I've never used this so use at your own risk.
$('confirm text').dialog(
{
modal:true, //Not necessary but dims the page background
buttons:{
'Save':function() {
//Whatever code you want to run when the user clicks save goes here
},
'Delete':function() {
//Code for delete goes here
}
}
}
);
This should work, but you will need to download or link to jQuery and the jQuery UI libraries to run this.