I want to confirm user before deleting a row from the table so here the anchor tag html code, which i am using
<a href="/Mpa/Orders/DeleteQuote/22" class="btn btn-danger">Delete Quote</a>
Now, how to show confirmation dialog box("Are you sure, you want to delete it?") using javascript -jquery or HTML, before taking the action of delete or calling back end, if user clicks on above anchor tag?
Thanks
You can call Confirmation box by simply add onclick
event handler in a href(anchor ), so your code will be something like this
<a href="/Mpa/Orders/DeleteQuote/22" class="btn btn-danger" onclick="return confirm('Are you sure, you want to delete it?')">Delete Quote</a>
If user clicks on "OK" then only href="url" will be called.
swal("Click on either the button or outside the modal.")
.then((value) => {
swal(`The returned value is: ${value}`);
});
Fiddle link: https://jsfiddle.net/90L124n8/
You need to include these CDN links
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js" ></script>
Here is the fiddle link answer's from the facebook group link
By Bogdan Suciu https://jsfiddle.net/nqkco3ov/3/
By Nicholas Rios https://jsfiddle.net/kqzq293s/
both links work as expected, but the second answer(by Nicholas Rios) is preferred so here the code for it
<table>
<tbody>
<tr><th>Row1 <a class="delete" href="#">Delete row</a></th></tr>
<tr><td>Row2 <a class="delete" href="#">Delete row</a></td></tr>
<tr><td>Row3 <a class="delete" href="#">Delete row</a></td></tr>
</tbody>
</table>
Javascript:
function addListeners(){
var links = document.querySelectorAll('.delete');
for(i = 0; i < links.length; i++){
var currentLink = links[i];
currentLink.addEventListener('click', clickHandler);
}
}
function clickHandler(e){
e.preventDefault();
var elm = e.target;
var confirmation = confirm('Are you sure you want to delete this row?');
if(confirmation){
elm.parentElement.parentElement.remove();
return true;
}else{
return false;
}
}
addListeners();
thanks
Another solution Using jQuery you can call confirm on anchor click like below
$('a').click(function(){
return confirm('Are you sure want to continue?');
});
If someone stumble upon this and looking for Bootstrap confirmation pop-up Modal before delete in asp .net MVC
Please take a look at recently added article related to this: https://qawithexperts.com/article/asp-net/show-bootstrap-confirmation-modal-before-delete-in-aspnet-mv/230
Thanks
Using Javascript to show Confirmation on click on all anchor links of page.
var anchorLink = document.getElementById('checkConfirmation');
anchorLink.onclick = function() {
var check = confirm("Are you sure you want to leave?");
if (check == true) {
return true;
}
else {
return false;
}
};
Considering HTML
<a href="https://qawithexperts.com" id="checkConfirmation">Navigate to other website</a>
fiddle: https://jsfiddle.net/fvkago0n/
It will ask the user before redirecting to the website.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly