• We just launched and are currently in beta. Join us as we build and grow the community.

JavaScript - Simple Confirm Message Box

Toiy

Script Execution Specialist
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
160
Likes
138
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    Javascript - Simple Confirm Message Box</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <form

    action

    =

    "javascript:void(0);"

    method

    =

    "POST"

    class

    =

    "form-inline"

    onsubmit

    =

    "Add()"

    >
  18. <label

    >

    Country:</

    label

    >
  19. <input

    type

    =

    "text"

    id

    =

    "country"

    class

    =

    "form-control"

    /

    >
  20. <button

    type

    =

    "submit"

    class

    =

    "btn btn-primary form-control"

    ><span

    class

    =

    "glyphicon glyphicon-plus"

    ></

    span

    >

    Add</

    button

    >
  21. </

    form

    >
  22. <br

    /

    ><br

    /

    >
  23. <table

    class

    =

    "table table-bordered"

    >
  24. <thead

    class

    =

    "alert-info"

    >
  25. <tr

    >
  26. <th

    >

    Country</

    th

    >
  27. <th

    >

    Action</

    th

    >
  28. </

    tr

    >
  29. </

    thead

    >
  30. <tbody

    style

    =

    "background-color:#fff;"

    id

    =

    "result"

    ></

    tbody

    >
  31. </

    table

    >
  32. </

    div

    >
  33. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  34. </

    body

    >
  35. </

    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. displayData(

    )

    ;

  4. function

    displayData(

    )

    {
  5. var

    content =

    ''

    ;
  6. if

    (

    countries.length

    >

    0

    )

    {
  7. for

    (

    i =

    0

    ;

    i <

    countries.length

    ;

    i++

    )

    {
  8. content +=

    '<tr>'

    ;
  9. content +=

    '<td>'

    +

    countries[

    i]

    +

    '</td>'

    ;
  10. content +=

    '<td colspan="2"<button class="btn btn-danger" onclick="Delete('

    +

    i +

    ')"><span class="glyphicon glyphicon-trash"></span> Delete</button></center></td>'

    ;
  11. content +=

    '</tr>'

    ;
  12. }
  13. }

  14. result.innerHTML

    =

    content;
  15. }

  16. function

    Add(

    )

    {
  17. var

    el =

    document.getElementById

    (

    'country'

    )

    ;
  18. var

    country =

    el.value

    ;
  19. if

    (

    country)

    {
  20. countries.push

    (

    country.trim

    (

    )

    )

    ;
  21. el.value

    =

    ''

    ;
  22. displayData(

    )

    ;
  23. }
  24. }

  25. function

    Delete(

    item)

    {
  26. if

    (

    confirm(

    'Are you sure you want to delete this record?'

    )

    )

    {
  27. countries.splice

    (

    item,

    1

    )

    ;
  28. displayData(

    )

    ;
  29. }

    else

    {
  30. return

    false

    ;
  31. }
  32. }

⚙ Live Demo

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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,158

323,328

323,337

Top