I am writing a function to delete some records. on deletion there is an alert message with 'yes' and 'no' to which user needs to click to delete the record.
The problem is that I want to pass yes to this confirmation without manually clicking on the yes. how can I do this. please provide pointers.
Not saying you should do the following, but here's how one might try to bypass confirmation dialogs.
Replace the confirm function with your own function that returns true.
var realConfirm = window.confirm;
window.confirm = function () { return true; }
deleteRows();
window.confirm = realConfirm;
If you are talking about native javascript's confirm() then, you cannot do what you intend to. If you override the function, you'll not get the popup behaviour.
But if you have a custom alert box kind of thing, then assign an id to the Yes or Ok button.
Then you can do this:
document.getElementById('yesId').click(); //this will trigger a click.
And yes, as Will Newton said, if you don't wanna press the button, just use Return or Enter button.
Related
Hi guys i'm currently working on a delete validate function in javascript. Here is my code.
function confirmDelete(){
var agree = confirm("Are you sure you want to delete this file?");
if(agree == true){
return true
}
else{
return false;
}
}
The alert dialog box comes out but whenever i press "Cancel" button it still deletes the file in the table
Here is my HTML:
<a href=".base_url()."main/delete?id=$row->id>"."<i class='icon-trash' rel = 'tooltip' title = 'Delete' id = 'delete' onClick = 'confirmDelete();'></i></a>";
I'm using CodeIgniter by the way any help will much be appreciated! Thanks guys! :)
try to use return statement where you are calling javascript function.
Like below:
<a href=".base_url()."main/delete?id=$row->id>"."<i class='icon-trash' rel = 'tooltip' title = 'Delete' id = 'delete' onClick = 'return confirmDelete();'></i></a>";
Just do it directly inside your tag:
Delete
Because the on click should be in the <a>, not the <i>. Also, your function could be simplified, considering confirm returns true or false:
function confirmDelete(){
return confirm("Are you sure you want to delete this file?");
}
Call event.preventDefault(), if returning false on its own is not preventing the browser action.
You will need to get a reference to event or window.event.. which is somewhat browser-specific. jQuery or similar can help with this.
onclick="if (! confirm('Are you sure you want to delete this book?')) event.preventDefault();"
(Though, with the above example, we're not returning 'false' any more -- ideally, we'd put the code in a method, and both return false and prevent default if the confirmation is negative.)
To test if it works, most simply:
onclick="event.preventDefault();"
See also:
jQuery: How to get the event object in an event handler function without passing it as an argument?
http://www.quirksmode.org/js/events_access.html
event.preventDefault() vs. return false
The difference between returning 'false' and e.preventDefault() will prevent the default event from occuring,e.stopPropagation()` prevents it from bubbling up/ outwards & outer defaults occurring, but return 'false' does not stop it from bubbling.
So perhaps it would be better to put the onclick handler on the link, which is performing the default action?
I am looking the way to stop the progress of button click until confirmation had made.
Please advice a way to stop the progress temporary until 'Show Confirm Box' return true. Now my function will keep running forward regardless it.
.click(function(){
//Show Confirm Box <- will return true of false
//Start Process if true;
});
Thank you very much.
Calls to confirm() are synchronous. That is, until the user closes the box, your code is effectively paused.
if (confirm('some message')) {
// User confirmed, do something here
}
Now, if you are not using confirm(), and are instead using your own custom modal dialog or something similar, then you will need to use callbacks.
You should not block your script from executing, as the page will appear locked up to the user. Instead, pass a callback function to your function that shows your dialog, and let your dialog call that function when you are done.
function showDialog (confirmCallback) {
// Show dialog here
if (result === 'yes') { // replace this, obviously
confirmCallback();
}
}
.click(function(){
showDialog(function () {
// start process
});
});
The parameter to the click event is a function handler which will get executed when the click event occurs.
So You can always return from that function when the confirmation dialog is returned a false value.
Code will be like this
jQuery(".button").click( function(){
var ans = confirm("Do you want to proceed further ?");
if(!ans) return;
alert("Now you can code the rest ");
});
I've created a fiddle , check this below
http://jsfiddle.net/shidhincr/Ubj7S/1/
did you see this question?
pausing execution in custom confirm box
just split up the code and call the functions according to the users input from the confirm box
Is there a way to capture the alert ok button click event? In jQuery?
The alert() function is synchronous and you can't verify what was clicked (it does not return anything), so the code below the call will be executed after it is closed (ok or close button). The alert is not used to gain user input. It is an alert, a message to the user. If you need to check what the user want, you should use confirm(). Note that the function name tells its purpose like alert.
Something like:
// if the ok button is clicked, result will be true (boolean)
var result = confirm( "Do you want to do this?" );
if ( result ) {
// the user clicked ok
} else {
// the user clicked cancel or closed the confirm dialog.
}
Alert is a blocking function, means, if you don't close it, the code below will not execute.
So you don't have to capture the alert close event, just write down the code below that alert, when alert window will be closed the code below will be executed automatically.
See example below:
alert("Close Me");
// Write down the code here, which will executed only after the alert close
console.log("This code is executed after alert")
Disclaimer: This is a very bad thing to do.
Technically you could hook into it with this code:
window.alert = function(al, $){
return function(msg) {
al(msg);
$(window).trigger("okbuttonclicked");
};
}(window.alert, window.jQuery);
$(window).on("okbuttonclicked", function() {
console.log("you clicked ok");
});
alert("something");
Demo: http://jsfiddle.net/W4d7J/1/
There is no event for the window.alert(). Basically the next line after it is called when they click ok. I am not sure why you would need to listen for it.
I tried this in a site I created and it worked perfectly :
<< Back
You could use JAlert and assign a click handler to the ok button.
Something like
jAlert("Alert message goes here.");
$('#popup_ok').bind('click',function(){
//Do operation after clicking ok button.
function_do_operation();
});
This the code for alerting some value:
alert('Click the OK button Now !');
So now i want to check whether the OK button is clicked or not.
How can I do this using this JavaScript?
Confirm could work:
var r=confirm("Click the OK button now!");
if (r==true)
{
alert("You pressed OK!");
}
else
{
alert("You pressed Cancel!");
}
Confirm HAS to have an OK and Cancel button. If you only want one button, you should either use the alert() method (which doesn't tell you if the OK was clicked) or you should look into something like the jQueryUI Dialog control.
The jQueryUI dialog is a bit more complicated because you need to include some extra JavaScript libraries and do a bit of extra wiring up to get it to work. There are a lot of examples to follow here.
Sounds like you might want a confirm box instead of an alert:
http://www.w3schools.com/js/js_popup.asp
This returns true or false depending on what the user presses. Alert does not return a value.
Use a jquery dialog then you can post or check what every you want from a range of buttons. Much more flexible
Jquery Modal
As mentioned previously, confirm() is the best bet, however don't forget you can check which button was pressed, and ask for a value at the same time using prompt().
if (prompt("Click the OK button?")!=null)
{
alert('you clicked OK and entered a value')
}
else
{
alert('you clicked cancel')
}
I'm using SimpleModal (http://www.ericmmartin.com/projects/simplemodal/) and I have a form that displays in a dialog. What I want to do is be able to have a confirmation come up each time that the user tries to close the dialog (either by escape or clicking on the close icon) and asks them if they really want to close it without saving the form data. I tried the following:
onClose: function (dialog) {
if (confirm('Are you sure you want to close without saving?')) {
$.modal.close();
}
}
But it only triggers once. If you hit cancel then fails to close again later, which kind of makes sense. Anybody have a suggestion or solution? Any help would be greatly appreciated. :)
I looked at the source of SimpleModal for you and what you are wanting to do can't be done with their code. This is why:
Just prior to calling your custom callback onClose it calls this:
s.unbindEvents();
Which effectively says "This box is going to close whether you like it or not". It is not like a normal callback which you can cancel.
I would recommend instead using the jQuery UI Dialog, which you should find super easy to implement that functionality by using their beforeclose callback. You would simply use:
beforeclose: function(){
return confirm('Are you sure you want to close without saving?')
}
I got this working using sort of what jerone was trying but also rebinding the events:
onClose: function (dialog) {
if (confirm('Are you sure you want to close without saving?')) {
$.modal.close();
}else{
this.occb = false;
this.bindEvents();
}
}
This plugin needs to be updated to support the cancel of the close event. It looks like its not really being thought of as a event in the code. I would like it to behave just like any other js event.
I've also looked at the source and when the onclosed event is executed a occb flag is enabled.
What you can try (I haven't tried it) is to override this occb as it's passed to the this variable:
onClose: function (dialog) {
if (confirm('Are you sure you want to close without saving?')) {
$.modal.close();
}else{
this.occb = false;
}
}
Hope this helps.