Hello friends!
Please have a look at the functionality of all these three types of popup available in JavaScript.
Alert
Confirm
Prompt
For creating custom pop-ups, we need to include jQuery and Bootstrap. I have used a CDN path for this. You can also install this with npm or just use this CDN.
Here is the code of Index.html file.
- <html>
- <head>
- <title>Bootstrap Example</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <style>
- .btns {
- margin: 250px auto 50px;
- width: 600px;
- }
- </style>
- <body>
- <div class="btns text-center">
- <button type="button" class="btn btn-warning" onClick="createModal('We aware', 'some text here which want to show as warning..', 'alert')">Alert Modal</button>
- <button type="button" class="btn btn-primary" onClick="createModal('Enter your name', 'Name', 'prompt')">Prompt Modal</button>
- <button type="button" class="btn btn-danger" onClick="createModal('Are you sure!!', 'Some message here...', 'confirm')">Confirm Modal</button>
- </div>
- <!-- modal-dialog -->
- <div class="modal fade" tabindexrole="dialog" id="custom-modal">
- <div class="modal-dialog modal-sm" role="document">
- <div class="modal-content">
- <!-- modal-header -->
- <div class="modal-header" id="modal-head"></div>
- <!-- modal-body -->
- <div class="modal-body" id="modal-body"></div>
- <!-- modal-footer -->
- <div class="modal-footer" id="modal-footer"></div>
- </div>
- </div>
- </div>
- <h1 id="user-name" class="text-center"></h1>
- <script>
- function createModal(title, message, type) {
- customModal(title, message, type);
- }
- function customModal(head, body, type) {
- if(type == 'prompt') {
- $('#modal-head').html('
- <h4 class="modal-title">'+head+'</h4>');
- $('#modal-body').html('
- <div class="row">
- <label class="col-sm-3">Name</label>
- <div class="col-md-9">
- <input class="form-control" type="text" id="name-input">
- </div>
- </div>');
- $('#modal-footer').html('
- <button type="button" class="btn btn-primary" id="done-btn">Done</button>
- <button type="button" class="btn btn-danger" id="cancel-btn">Cancel</button>');
- $('#custom-modal').modal('show');
- $('#cancel-btn').on('click', function() {
- return response('cancel');
- });
- $('#done-btn').on('click', function() {
- return response('done');
- });
- } else if( type == 'alert') {
- $('#modal-head').html('
- <h4 class="modal-title">'+head+'</h4>');
- $('#modal-body').html('
- <p>' + body + '</p>');
- $('#modal-footer').html('
- <button type="button" class="btn btn-primary" data-dismiss="modal">Ok</button>');
- $('#custom-modal').modal('show');
- } else if( type == 'confirm') {
- $('#modal-head').html('
- <h4 class="modal-title">'+ head +'</h4>');
- $('#modal-body').html('
- <p>' + body + '</p>');
- $('#modal-footer').html('
- <button type="button" class="btn btn-primary" id="ok-btn">Ok</button>/
- <button type="button" class="btn btn-danger" id="cancel-btn">Cancel</button>');
- $('#custom-modal').modal('show');
- $('#cancel-btn').on('click', function() {
- return response('cancel');
- });
- $('#ok-btn').on('click', function() {
- return response('ok');
- });
- }
- }
- function response(type) {
- if(type == 'done') {
- if(document.getElementById('name-input').value != '') {
- $('#user-name').html( 'Welcome '+ document.getElementById('name-input').value);
- $('#custom-modal').modal('hide');
- return document.getElementById('name-input').value;
- } else {
- alert('Please Enter your name');
- }
- } else if(type == 'cancel') {
- $('#custom-modal').modal('hide');
- return false;
- } else if(type == 'ok') {
- $('#custom-modal').modal('hide');
- return true;
- }
- }
- </script>
- </body>
- </html>
Here, you can see the screenshots which show all the three types of custom pop-ups developed by us.
Initial loading screen
Alert Box popup

Prompt Box popup

Confirm Box popup



Ronald HaringPosted Feb 27, 2021, 9:35 AM EST
Thanks any chance you have it somewhere as git or other copy-able source?
Jeffrey RudeloffPosted May 2, 2020, 5:27 AM EDT
Nice bit of coding and exactly what I was looking for! Thanks...
Sudheshwer RaiPosted May 18, 2019, 3:07 AM EDT
Nice airticle