1

I've created a confirm dialog function that accepts a function which will be triggered when the user clicks "accept".

Now I would like to add a callback to this passed function so I can render a loader of some kind to let the user know the system is working on the response.

Here's the code so far;

confirmDialog('Confirm deletion', 'Are you sure you want to delete the internet?', function(){alert('test');});

function closeConfirmDialog() {
  var dialog = document.getElementById('confirmDialog');
  var overlay = document.getElementById('dialogOverlay');
  if (dialog) { document.getElementsByTagName('BODY')[0].removeChild(dialog); }
  if (overlay) { document.getElementsByTagName('BODY')[0].removeChild(overlay); }
}
function confirmDialog(title, message, confirmCallback) {
  // Closing any active confirmDialog 
  closeConfirmDialog();
  // Create all required elements for the dialog box
  var dialogOverlay = document.createElement('div');
  var dialogBox = document.createElement('div');
  var buttonBox = document.createElement('div');
  var titleBox = document.createElement('div');
  var textBox = document.createElement('div');
  var cancelButton = document.createElement('a');
  var successButton = document.createElement('a');
  // Add classes/content to elements
  dialogOverlay.setAttribute('id', 'dialogOverlay');
  dialogOverlay.onclick = function () { closeConfirmDialog(); };
  dialogBox.setAttribute('id', 'confirmDialog');
  buttonBox.className = 'buttonBox';
  titleBox.className = 'titleBox';
  titleBox.innerHTML = title;
  textBox.className = 'textBox';
  textBox.innerHTML = message;
  cancelButton.className = 'dangerbtn left';
  cancelButton.innerHTML = 'Cancel';
  cancelButton.onclick = function () { closeConfirmDialog(); };
  successButton.className = 'successbtn right';
  successButton.innerHTML = 'Confirm';
  /**
         * This is where I would like to modify the confirmCallback function to always show a loader.
         */
  // Here is where I set the callback to the successButton onclick event.
  successButton.onclick = confirmCallback;
  // Add all elements to their respective wrappers
  buttonBox.appendChild(cancelButton);
  buttonBox.appendChild(successButton);
  dialogBox.appendChild(titleBox);
  dialogBox.appendChild(textBox);
  dialogBox.appendChild(buttonBox);
  // Append the overlay and finished confirmbox to the body.
  document.getElementsByTagName('BODY')[0].appendChild(dialogBox);
  document.getElementsByTagName('BODY')[0].appendChild(dialogOverlay);

  return false;
}
body {
  font-family: arial;
}
#confirmDialog {
  position: absolute;
  top: 10%;
  left: 25%;
  width: 50%;
  border: 1px solid #000;
  padding: 10px 10px 0 10px;
  box-sizing: border-box;
}
#confirmDialog>.titleBox {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 4px;
}
#confirmDialog>.textBox {
  margin-bottom: 4px;
}
#confirmDialog>.buttonBox {
  border-top: 1px solid #000;
  padding: 5px 10px;
  margin: 0 -10px;
  float: left;
  width: 100%;
}
#confirmDialog>.buttonBox>.dangerbtn {
  background-color: #d9534f;
  border-color: #d9534f;
  color: #fff;
  padding: 4px 12px;
  float: left;
  cursor: pointer;
}
#confirmDialog>.buttonBox>.successbtn {
  background-color: #5cb85c;
  border-color: #5cb85c;
  color: #fff;
  padding: 4px 12px;
  float: right;
  cursor: pointer;
}

5
  • Can you show how you would invoke confirmDialog? Commented Dec 22, 2016 at 10:36
  • It's in the snippet; confirmDialog('Confirm deletion', 'Are you sure you want to delete the internet?', function(){alert('test');}); Commented Dec 22, 2016 at 10:37
  • So, instead of alert(test) write the stuff you need (display the loader etc) Commented Dec 22, 2016 at 10:39
  • Every single time it's invoked in the project? Commented Dec 22, 2016 at 10:40
  • If you want the loader to be shown for all dialogs, then add it to the event handler: successButton.onclick = function() { loader(); setTimeout(confirmCallback,1) } Commented Dec 22, 2016 at 10:44

1 Answer 1

1

If you just want to show a loader (and nothing in the passed callback should care about it), change this line successButton.onclick = confirmCallback; to:

successButton.onclick = function() {
  confirmCallback();
  showLoader();
}

But if you want to pass some data into callback (and it may be a showLoader function, if you want to have some logic in the callback) — just pass it into your callback, like this:

successButton.onclick = function() {
  confirmCallback(showLoader);
}

But don't forget to call it in your callback!

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, yes. I completely forgot to add the parenthesis to the confirmCallback variable... Accepting answer in a minute. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.