The Alert dialog box stops the whole process until the user gets a mouse response on its buttons. To Show, the custom alert box you have to use a library like – You can use jQuery UI Dialog. In the tutorial, you will learn how to create a JavaScript alert Yes No button option.
Before start you must read this tutorial to know about JavaScript alerts – JavaScript Alert Hello World | Alert box.
Javascript confirm Yes No Alert Dialog example
You can’t do that with the native confirm()
as it is the browser’s method.
You have to create a plugin for a confirm box (or try one created by someone else). And they often look better, too.
Jquery libraries create HTML elements that look and behave like a dialog. You can put anything you want (including form elements or video or images) in the dialog.
Here is example code for it:-
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
function functionConfirm(msg, myYes, myNo) {
var confirmBox = $("#confirm");
confirmBox.find(".message").text(msg);
confirmBox.find(".yes,.no").unbind().click(function() {
confirmBox.hide();
});
confirmBox.find(".yes").click(myYes);
confirmBox.find(".no").click(myNo);
confirmBox.show();
}
</script>
<style>
#confirm {
display: none;
background-color: #f2f4ef;
border: 1px solid #aaa;
position: fixed;
width: 250px;
height: 150px;
left: 50%;
margin-left: -100px;
padding: 8px;
box-sizing: border-box;
text-align: center;
}
#confirm button {
background-color: #4aace3;
display: inline-block;
border-radius: 5px;
border: 1px solid #aaa;
padding: 5px;
text-align: center;
width: 80px;
cursor: pointer;
margin-top: 20px;
}
#confirm .message {
text-align: left;
}
</style>
</head>
<body>
<div id="confirm">
<div class="message"></div>
<button class="yes">Yes</button>
<button class="no">No</button>
</div>
<button onclick = 'functionConfirm("Do you like Cricket?", function yes() {
alert("Yes")
},
function no() {
alert("No")
});'>submit</button>
</body>
</html>
Output:
Reference: https://stackoverflow.com
Q: How to create a dialog with a “yes” and “no” button in JavaScript?
Answer: You can create a dialog box with a “Yes” or “No” button option or any custom color and style. To create an Alert dialog with “Yes” or “No”, use a custom dialog box.
Do comment if you have any doubts and suggestions on this tutorial.
Note: The jquery confirm dialog yes no
Examples are tested on Safari browser (Version 12.0.2).
OS: macOS 10.14 Mojave
Code: HTML 5 Version