The following example shows an approach to creating a custom alert and handling the result of a user selection
function exampleAlert(message, successCallback) { var alertBox = document.createElement("div"); var msg = document.createElement("div"); msg.innerHTML = message; var btnYes = document.createElement("div"); btnYes.innerHTML= "Yes"; btnYes.onclick = function(){ $(this.parentNode).remove();successCallback();} var btnNo = document.createElement("div"); btnNo.innerHTML= "No" btnNo.onclick = function(){ $(this.parentNode).remove();} $(alertBox).append(msg, btnYes, btnNo).appendTo("body"); } function test() { alert("Example alert is working, don't use this test as a replacement test - horrible recursion!") } exampleAlert("shoe", test)
It is quite simple and does not allow adding additional data to the callback function and for this reason is not ideal for production, but jQuery.bind () and similar methods allow you to associate data with the callback method
It is worth noting that, although the above demonstrates the full implementation of the problem, in fact there are only two lines.
btnYes.onclick... btnNo.onclick...
Since we achieve the desired result by binding the onclick events for true and false, respectively, everything else should draw an image.
With this in mind, you can effectively turn any container object with at least one brother into a warning field for eaxmple:
<div id='a'> <ul> <ul> <li>Something</li> <li>Something Else</li> <li id='yesIdentifier'>Something not necessarily suggesting a trigger?</li> </ul> </ul> </div>
As long as your yes / no (if no exist) options destroy the corresponding container, converting the container to a warning field can be processed in a few lines of code.
$('#yesIdentifier', '#a').click( function(){ someCallback(); $(this).closest('#a').remove()});
None of the above examples are exemplary models for implementation, but should contain some ideas on how to complete the task.
Finally ... do you really need to replace your own notification method? That is, you write alert calls, in which case you know how to use your own method, or rewrite the default behavior, which you cannot guarantee that other developers will know.
General recommendation:
I believe that the best approach to this would be to create a jQuery plugin that will generate custom alerts on the fly and track callbacks, results, and what's not included in the plugin.
SOliver.