Javascript alert button OK button - javascript

I want to ask about javascript alert button.
Is it possible to do anything (e.g: redirect, clear form) after I clicked the OK button on the alert button?

You can just apply your function right after the alert:
alert('something');//execution is halted until ok button is pressed
foo(); // calls the foo method after ok button is pressed

You could change your alert to use a confirm window
var response = confirm('Are you sure you want to clear the form?');
if (response){
// clear the form
console.log('clearing the form approved');
}
The confirm window is similar to the alert except it shows OK and Cancel buttons. It returns a bool result depending on the user's choice.
Like the alert window, it halts program/script execution until the user makes a decision

Related

Javascript - Confirm box to redirect user when they click on exit button

My objective is to when the user clicks on the exit button, it should appear a box with 2 buttons, to confirm or cancel the leave. After the confirm button is clicked, a redirect is needed.
I tried a lot of solutions, but I could only find a solution that I can control what happens after the confirm button.
But I need to do some action after they click on the confirm
My code:
window.onbeforeunload = function () {
return "message"
}
That opens a box to confirm the exit, but I can't control the actions after it
something like this
const result = confirm('Do this?');
if(result){
//redirect here
}

My confirm popup is still deleting an object whether ok or cancel is clicked. Why? Angular to blame?

I'm trying to create a popup that gives a user the option of hitting ok or cancel upon clicking a delete button. However, even if the user presses cancel it still deletes it. Not sure what I can do about that. Is it some weird thing about using AngularJS? Here's my code:
<button onclick="return confirm('Are you sure?');" ng-click="vm.deleteTask(task)">Delete</button>
No angular is not to blame.
You are using ng-click and onclick & both of them will independently work.
Use only ng-click
<button ng-click="vm.deleteTask(task)">Delete</button>
Inside this function deleteTask call the confirm
deleteTask = function(param){
var confirmStatus = confirm('Are you sure?');
// will be trur if ok button is pressed or false
if(confirmStatus){
// code to delete the task
}
else {
// do what ever
}
}

deselect the alert in javascript

I am working on some project and I have used alert type function in my project.
I want to do a task is that if I cross or cancel the alert box then i don't want to continue the task
<td><input type="submit" Onclick="myfunction()" value="Delete"> <input type="reset" value="Reset"></td>
<script>
function myfunction() {
alert("You are trying to delete faculty record permanently");
}
</script>
In above code if i cancel the alert box then i want to return on previous page and if I click on OK on alert box then i want to perform task successfully
can anyone suggest me that how can i do this task??
alert() is for displaying a message. If you want to confirm something, you can use the aptly named confirm():
if(confirm("Should we do it?")) {
alert("We did it!");
} else {
alert("Or not.");
}
You will also probably want to attach this handler to the onsubmit event of the form. In fact, simply setting onsubmit="confirm('…')" will work; the return values match up.
alert will just alert text (message) to the user, it doesn't expect any input or interactions.
confirm on the other hand will check if the user want to proceed or cancel by letting the user choose between two buttons.
prompt will prompt the user to enter some text and then return it so it can be used.
Example:
In your case you need to use confirm like this:
var result = confirm("You want to continue?"); // confirm will return either true or false
if(result)
alert("Let's continue then!");
else
alert("Bye!");
You cannot do this with Window.alert() as it returns undefined. See https://developer.mozilla.org/en-US/docs/Web/API/Window/alert
I think what you're looking for is Window.confirm(). This returns a boolean depending on what button (typically "OK" or "Cancel") you clicked. See https://developer.mozilla.org/en-US/docs/Web/API/Window/confirm
result = confirm('You are trying to delete faculty record permanently')
if (result) {
alert('Delete record')
} else {
alert('Do not delete record')
}

Javascript confirm box not able to submit form on selecting yes

In jsp , clicking on button called sumbitForm() function as below
document.Data.formSubmit.value="Yes";
document.Data.action.value='SUMBIT';
document.Data.submit();
here giving proper result and setting value as occured on controller
In same JSP, calling onload function ,In that checking if command class variable set = occured then only confirmation box can show and after clicking yes button of confirmation box then request should process.. I used document.Data.submit() but its not working and not giving exception.
i think this will help you
if(confirm("Are you sure you want to submit the form ") == true ){
// then submit the form
}

return true or false based on button clicked in pop up window

Okay so with jQuery I've intercepted the .submit() of a form and I want to create a custom pop up window that shows them the data that the entered and asks them to confirm it. If they click the confirm button true is returned to .submit() and they continue but if false is pressed then they should not move on and have a chance to change their entry.
I already have the pop up window being made fine with the contents of the form being displayed and the buttons being shown. What I'm not sure how to do is bind the click functions of the buttons so that if one is clicked it returns false to .submit() and if the other is clicked true is returned to .submit()
If you need me to post some of my code just let me know.
I don't want to use a confirm dialogue since i would like it to be a custom pop up window.
You need to use a confirm() dialogue:
var submit = confirm('Are you sure?');
if (submit) {
$(this).submit();
}
else {
return false;
}
This works by the dialogue presenting the message "Are you sure?" to the user, if the user clicks on the confirmation ok button, the dialogue returns true to the variable submit, otherwise it returns false.
If false is returned (the user clicked cancel), then the if evaluates to false, and the else is executed.
You would need to pass the .submit() as a callback function to the dialogue. This isn't a one line solution but rather a pattern that you should get familiar with. This http://www.youtube.com/watch?v=hQVTIJBZook will probably be helpful for some of this topic along with other common issues that you may come across
Example:
function openPopup(form) {
//
// Put whatever code you use to open you
// popup here
//
// Bind click handler to submit form if they click confim
$("#id_of_confim_button").on("click", function() {
// Get the form that was
form.submit();
});
// Bind click handler for cancel button
$("#id_of_cancel_button").on("click", function() {
//
// Code to close your popup
//
});
};
$("#id_of_form_submit_button").on("click", function(event) {
// Stops the form from submitting
event.preventDefault();
// Get the form you want to submit
var form = $("#form_being_submit");
// Call your 'open custom popup' function and pass
// the form that should be submitted as an argument
openPopup(form);
});
Catching only this form's submits' click event won't handle all cases (f.ex. if someone hits enter on a non-textarea input, the form submits too).
If i want to handle submit in an asynchronous way, i used to fire manually submit after the original was prevented & bring in an isDirty state:
(function () {
var isDirty = true;
$("form#id").on("submit", function ( evt ) {
if (isDirty) {
evt.preventDefault();
popup( "... params ...", function () {
// this will called, when the popup ensures the form can be submitted
// ... & it will be called by the popup
isDirty = false;
$("form#id").submit();
} );
}
});
})();

Categories

Resources