HTML
Program for confirm OK and Cancel button |HTML|
Introduction
Have you ever wanted to prompt users with a confirmation message before they perform a certain action on your website? With JavaScript, you can easily create a confirmation dialog that gives users the option to proceed or cancel. In this blog post, we'll walk through creating a simple confirmation dialog with an "OK" and "Cancel" button.
Code
<html>
<head>
<title>Confirmation Dialog </title>
<script type="text/javascript">
function show_confirm() {
var r = confirm("Press a button");
if (r == true)
alert("You pressed OK");
else
alert("You pressed Cancel");
}
</script>
</head>
<body>
<input type="button" value="Click" onClick="show_confirm()"/>
</body>
</html>
Output
- We've defined a JavaScript function called show_confirm() that displays a confirmation dialog using the confirm() method.
- When the user clicks the button, the show_confirm() function is triggered.
- If the user clicks "OK", the confirm() method returns true, and an alert message saying "You pressed OK" is displayed.
- If the user clicks "Cancel" or closes the dialog, the confirm() method returns false, and an alert message saying "You pressed Cancel" is displayed.
Conclusion
By incorporating a confirmation dialog into your web pages using JavaScript, you can provide users with a clear indication of the consequences of their actions and give them the opportunity to confirm or cancel their choices. This can help prevent accidental actions and enhance the overall user experience on your website. Feel free to customize the confirmation message and button labels to suit your specific needs and preferences.
Post a Comment
0 Comments