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();
});
Related
In case the close (X) is pressed, Boxy doesn't wait for a confirmation. Below is an example describing my problem:
$('form .close').click(function(event) {
event.stopPropagation();
Boxy.confirm("Are you sure ?", function() {
alert('ok');
});
return false;
});
However, when the OK button is clicked, everything works as expected.
Why does this not work as expected in case the (X) is pressed?
Please see this example that I made for you: http://jsfiddle.net/972ak/
$('form .close').click(function(event) {
Boxy.confirm("Are you sure ?", function() {
alert('ok');
});
return false;
});
Boxy documentation says:
Boxy.confirm(message, callback, options)
Displays a modal, non-closeable dialog displaying a message with OK and Cancel buttons. Callback will only be fired if user selects OK.
http://onehackoranother.com/projects/jquery/boxy/
As I have already mentioned in my comment Boxy.confirm is async unlike native confirm. Your code will continue its execution without waiting for user to click OK or Cancel. That is why you need to perform the actual action inside confirm callback.
Consider the following code.
$('form .close').click(function(e){
var form = $(this).closest('form');
Boxy.confirm('Are you sure?', function() {
form.remove(); //remove it only if user confirmed.
});
form.append('<p>Close was clicked.</p>');
})
This code will append message every time user clicks close link. But the form will be actually removed only if user confirmed the action.
http://jsfiddle.net/tarabyte/972ak/4/
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.
In Safari Browser having one issue, following are my scenarios (with Example).
When i Click a button for delete an account on that time i opens alert message. in that alert window having two actions "OK" and "Cancel". if i click Yes it will redirect to another URL.(This is No Problem). But When i click "Cancel", i triggering to open another alert window. on that time previous alert not getting closed.
In Other Browsers like I.E, Firefox, Chrome it working fine.
Below is my Coding....
$('#upgradeNo').click(function(){ // Function
$('#accountFrame').hide('fast'); // To close the alert window( First alert)
$.modal.close(); // To close the alert window( First alert)
if( $("#deleteconf").val()=="ok"){ // Click Yes Button Function
deleteAcc();
}else{ // Click Cancel Button Function
$("#accountFrame").css('display','none');
deactivateAccount(); // new Alert gets open in this place
}
$("#deleteconf").val('');
});
Can anybody give me a quick solution. Awaiting for Response.
Thanks
i Got a Solution. i put the code in Modal Window OnClose Event. after that i called alert box. Now its working Fine , what i Expecting.
onClose: function (dialog) {
dialog.container.fadeOut(100, function () {
dialog.overlay.fadeOut(200, function(){
$.modal.close();
if (deactivateFlag)
{
deactivateAccount();
}
});
});
}
Thanks
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
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')
}