InterviewSolution
| 1. |
What is type of Operator in JavaScript? |
|
Answer» The following are the types of pop-up boxes available in JAVASCRIPT: Alert Box The alert box is for an alert or message to the user. User needs to click “OK”. The alert() method is used to add a message in the alert: Let us see an example: <!DOCTYPE html> <html> <body> <h1>Alerts in JavaScript</h1> <button onclick="show()">Display</button> <script> function show() { alert("This is an alert box!"); } </script> </body> </html>The output displays an alert on the click of a button: On clicking the above button, the following alert generates: Prompt Box To input a value from the user and display it, use the prompt box. Users need to click “OK” to return the entered input, else click “Cancel” to return null. Let us see an example: <!DOCTYPE html> <html> <body> <button onclick="show()">Display</button> <p id="myid"></p> <script> function show() { var res; var department = prompt("Enter your Department", "Operations"); if (department == null || department == "") { res = "Prompt cancelled!"; } else { res = "Department is " + department; } document.getElementById("myid").innerHTML = res; } </script> </body> </html>The output first displays the button which you need to click: On clicking the above button, a prompt box is visible WHEREIN user input is requested: On clicking “Ok”, the input is DISPLAYED on the web page: Confirm Box The confirm box is used in JavaScript to take user’s consent or accept something. User need to click “OK” to return true, else click “Cancel” to return false. Let us see an example: <!DOCTYPE html> <html> <body> <button onclick="show()">Display</button> <p id="myid"></p> <script> function show() { var res; if (confirm("Press a button!")) { res = "OK is pressed!"; } else { res = "Cancel is pressed!"; } document.getElementById("myid").innerHTML = res; } </script> </body> </html>The following is the output. The button is visible. You need to click it: On clicking the above button, a confirm box generates that WANTS users’ consent on clicking the button: On PRESSING OK, the following is visible: |
|