- Confirm dialogue box in java script used to display a message for an action weather its is required to perform or not.
- JavaScript confirm dialogue box contains two buttons "Ok" and "Cancel".
- If user clicks on OK button confirm dialogue box returns true. If user clicks on cancel button it returns false.
- So based on the user entered option we can continue our program. So confirm dialogue box used to test an action is required or not from user.
- Its not possible to change values of confirm dialogue box buttons from "Ok" and "Cancel"to "Yes" and "No".
- We need to use custom popups or jquery popups to use "Yes" and "No".
- var res= confirm("Are you sure to continue?");
How to use Javascript confirm dialogue box:
- function myFunction() {
- var text;
- var res= confirm("Are you sure to continue?");
- if (res == true) {
- text= "You clicked OK!";
- } else {
- text= "You clicked Cancel!";
- }
- document.getElementById("demo").innerHTML = txt
- }
- </script>
Javascript confirm delete onclick:
- If we are deleting a record from database then we need to ask user to confirm deletion if ye really want to delete record then user checks Ok otherwise cancel based on this we can proceed furthur.
- To implement this functionality we need to call a JavaScript function onClick() whenever user clicks on delete record button.
Program#1: JavaScript program to show confirm dialogue box on clicking delete student record.
- <!DOCTYPE html>
- <html>
- <body>
- <p>Click the button to delete student record</p>
- <button onclick="toConfirm()">Delete student record </button>
- <p id="student"></p>
- <script>
- function toConfirm() {
- var text;
- var r = confirm("You clicked on a button to delete student recored. Clik ok ro proceed");
- if (r == true) {
- //code to delete student record.
- text = "You clikced on ok. Student record deleted";
- } else {
- text = "You clicked on cancel. transaction cancelled.";
- }
- document.getElementById("student").innerHTML = text;
- }
- </script>
- </body>
- </html>
- If we click on Ok then it will delete student record. otherwise it wont.
No comments