Created
February 16, 2013 04:20
-
-
Save hoandang/4965527 to your computer and use it in GitHub Desktop.
Create a custom dialog (there are two buttons OK and Cancel leverage twitter bootstrap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', function() { | |
document.getElementById("click").addEventListener("click", function(e){ | |
var msg = 'Are you sure you want to delete action?'; | |
customConfirm(msg); | |
//customConfirm(msg, function(){alert('id deleted')},function(){alert('user pressed Cancel button')}); | |
e.preventDefault(); | |
}); | |
}); | |
function customConfirm(message, true_func, false_func){ | |
var container = document.createElement('div'); | |
//template for modal window | |
container.innerHTML += '<div class="modal custom-confirm">'+ | |
'<div class="modal-body">' + | |
'<div>' + message + '</div>' + | |
'<div class="controls">'+ | |
'<button type="button" class="btn primary">OK</button>' + | |
'<button type="button" class="btn">Cancel</button>' + | |
'</div>' + | |
'</div>' + | |
'</div>'; | |
//modal window | |
var modal = container.firstChild; | |
container = document.createElement('div'); | |
container.innerHTML = '<div class="modal-backdrop in"></div>'; | |
//dark background | |
var background = container.firstChild; | |
//get click OK button | |
var ok = modal.getElementsByTagName('button')[0]; | |
ok.className = "btn btn-remove"; | |
ok.onclick = function() { | |
modal.parentNode.removeChild(modal); | |
document.body.removeChild(background); | |
true_func(); | |
} | |
//get click Cancel button | |
var cancel = modal.getElementsByTagName('button')[1]; | |
cancel.className = "btn"; | |
cancel.onclick = function() { | |
modal.parentNode.removeChild(modal); | |
document.body.removeChild(background); | |
false_func(); | |
} | |
document.body.appendChild(background); | |
document.body.appendChild(modal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment