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>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In html we defined h3 tag with heading text ‘ALERT WITH YES/NO’ and div tag ‘res’ defined for appends result message on webpage.
- 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’.
- Using if condition we checks user response for confirm() method whether user clicked ‘ok’ or ‘cancel’.
- 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.
- 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.
- 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.