JavaScript - Simple Confirm Message Box

Submitted by: 
Language: 
Visitors have accessed this post 309 times.

In this tutorial we will create a Simple Confirm Message Box using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascript - Simple Confirm Message Box</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <form action="javascript:void(0);" method="POST" class="form-inline" onsubmit="Add()">
  17. <label>Country:</label>
  18. <input type="text" id="country" class="form-control"/>
  19. <button type="submit" class="btn btn-primary form-control"><span class="glyphicon glyphicon-plus"></span> Add</button>
  20. </form>
  21. <br /><br />
  22. <table class="table table-bordered">
  23. <thead class="alert-info">
  24. <tr>
  25. <th>Country</th>
  26. <th>Action</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;" id="result"></tbody>
  30. </table>
  31. </div>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </html>

Creating the Script

This code contains the script of the application. This code will display prompt to confirm the user actions. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. var result = document.getElementById('result');
  2. var countries = [];
  3.  
  4. displayData();
  5.  
  6. function displayData(){
  7. var content = '';
  8. if (countries.length > 0) {
  9. for (i = 0; i < countries.length; i++) {
  10. content += '<tr>';
  11. content += '<td>' + countries[i] + '</td>';
  12. content += '<td colspan="2"<button class="btn btn-danger" onclick="Delete(' + i + ')"><span class="glyphicon glyphicon-trash"></span> Delete</button></center></td>';
  13. content += '</tr>';
  14. }
  15. }
  16.  
  17. result.innerHTML = content;
  18. }
  19.  
  20. function Add(){
  21. var el = document.getElementById('country');
  22. var country = el.value;
  23. if (country) {
  24. countries.push(country.trim());
  25. el.value = '';
  26. displayData();
  27. }
  28. }
  29.  
  30. function Delete(item){
  31. if(confirm('Are you sure you want to delete this record?')){
  32. countries.splice(item, 1);
  33. displayData();
  34. }else{
  35. return false;
  36. }
  37. }

There you have it we successfully created a Simple Confirm Message Box using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.