For the following code
<script type="text/javascript">
$(document).ready(function(){
$("#erase").click(function(){
confirm('Are you sure you want to erase it?');
var data = {
"post": 123,
};
$("#wrapper").load("<?php echo $url; ?>", data)
});
});
</script>
The above code returns a confirmatio window with two options Ok and Cancel when the user clicks on the erase link. But irrespective of what they select (Ok or Cancel) the load() function is getting called. How can I prevent the load function if the user is opting out the action?
You’re never testing confirm. You need to store the value of a
confirm()call then, of course, test against it:The short-hand to the above would be avoiding an assignment first (which is also perfectly acceptable):
There’s a great write up on MDN about
window.confirmwith example usage.