java script alert yes no – How to create a dialog with “yes” and “no” options?

java script alert yes no – Alert Yes No With the confirm() Function in JavaScript. You can create a JavaScript confirmation box that offers “yes” and “no” options by using the confirm() method.

java script alert yes no

The confirm method takes a single string argument, and you can pass a message which you want to display in a dialog box.

How to create a dialog with “yes” and “no” options?

if (confirm('Are you sure you want to save this thing into the table?')) {
  // Update it!
  console.log('Thing was saved to the table.');
} else {
  // Do nothing!
  console.log('Thing was not saved to the table.');
}

Create a Dialog Using the confirm() Function in JavaScript

var dialog = confirm("Update data?");
if (dialog) {
    console.log('Data Saved')
}
else {
    console.log('Data Not Saved')
}

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <button type="button" onclick="if(confirm('Save Data?')){ console.log('Data Saved')};">SomeButton</button>
</body>
</html>

Example

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>JavaScript confirmation box - www.pakainfo.com</title>
    <script>
      // The function below will start the confirmation dialog
      function checkConfrmAct() {
        let checkConfrmAct = confirm("Are you sure to execute this action?");
        if (checkConfrmAct) {
          alert("Action successfully executed");
        } else {
          alert("Action canceled");
        }
      }
    </script>
  </head>
  <body>
    <h2>Call the confirmation box - Example</h2>
    <button onclick="checkConfrmAct()">Delete</button>
  </body>
</html>

Don’t Miss : Javascript Confirm Delete Yes No

Read Also:  javascript json decode - How to Parse JSON in javascript?

How to create a dialog with “yes” and “no” options in JavaScript?

index.html

<!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 sendRequest = $("#sendRequest");
            sendRequest.find(".message").text(msg);
            sendRequest.find(".yes,.no").unbind().click(function() {
               sendRequest.hide();
            });
            sendRequest.find(".yes").click(myYes);
            sendRequest.find(".no").click(myNo);
            sendRequest.show();
         }
      </script>
      <style>
         #sendRequest {
            display: none;
            background-color: #e60000;
            border: 1px solid #aaa;
            position: fixed;
            width: 250px;
            left: 50%;
            margin-left: -100px;
            padding: 6px 8px 8px;
            box-sizing: border-box;
            text-align: center;
         }
         #sendRequest button {
            background-color: #3d3d3d;
            display: inline-block;
            border-radius: 5px;
            border: 1px solid #aaa;
            padding: 5px;
            text-align: center;
            width: 80px;
            cursor: pointer;
         }
         #sendRequest .message {
            text-align: left;
         }
      </style>
   </head>
   <body>
      <div id="sendRequest">
         <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>

I hope you get an idea about java script alert yes no.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this Article are always welcome.
If you enjoyed and liked this post, don’t forget to share.