All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Alert Yes No

Last Updated : Mar 11, 2024

JavaScript Alert Yes No

In this tutorial we will show you the solution of JavaScript alert yes no, here we needs to use confirm() method this is used to display a model dialog with an optional message and two buttons ok and cancel. It return true if the user clicks ‘OK’ and false otherwise.

So using that we displaying message on webpage and also alert box based on user response for confirm() method dialog box texts.

Step By Step Guide On JavaScript Alert Yes No :-

Here we defined h3 tag for display concept heading and div tag defined for appends result on webpage.

In script we defined variable ‘a’ for refer confirm() method with text message ‘save’ then using if condition we checks user response for confirm() method whether ‘true’ or ‘false’.

If they cancel that response will false so respective message will displayed on webpage otherwise user response will true respective message will displayed on webpage.

<!DOCTYPE html>
<html>
    <head>
        <title>ALERT WITH YES/NO</title>
    </head>
    <body>
        <h3>ALERT WITH YES/NO</h3>
        <div id="res"></div>
        <script>
            a=confirm("save");
            if(a==true){
                alert("User proceed ok");
                document.getElementById("res").innerHTML="User proceed ok";
            }
            if(a==false){
                alert("User proceed cancel");
                document.getElementById("res").innerHTML="User proceed cancel";
            }
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In html we defined h3 tag with heading text ‘ALERT WITH YES/NO’ and div tag ‘res’ defined for appends result message on webpage.
  7. In <script> we defined variable ‘a’ for refers confirm(‘save’) method with defined message it throws we passed message on alert box with buttons ‘ok,cancel’.
  8. Using if condition we checks user response for confirm() method whether user clicked ‘ok’ or ‘cancel’.
  9. If they clicks ‘ok’ then condition ‘a==true’ will executed so alert box with message ‘User proceed ok’ will appear after closing alert box same result message will displayed on webpage.
  10. If they clicks ‘cancel’ then condition ‘a==false’ will executed so alert box with message ‘User proceed cancel’ will appear after closing alert box same result message will displayed on webpage.
  11. Both </body>, </html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to handle alert yes or no using javascript.

When we executing this program on browser we can see alert box with ‘save’ message will appeared then user needs to clicks on ‘ok or cancel’.

If user clicks ‘ok’ then ‘User proceed ok’ message will appear on alertbox and also on webpage otherwise ‘User proceed cancel’ message will appear on alertbox and also on webpage.

I hope this tutorial on JavaScript alert yes no helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪