Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to confirm clicking on a link using jQuery

I have many links in a HTML-table, which delete corresponding row, when clicked (calling a PHP-script via GET parameter).

They all have a class delete_row.

How could I please display a confirm('Really delete?') dialog using jQuery, when such a link is clicked?

And of course prevent following that link when No has been selected in the dialog.

like image 465
Alexander Farber Avatar asked Jan 28 '12 11:01

Alexander Farber


People also ask

How do you check whether a link is clicked or not in jQuery?

In jQuery if I want to check if the user clicked any link in page I use this code: $('a'). click(function() { // do something });

How do you show confirmation dialog when clicking on a link?

In the most simple way, you can use the confirm() function in an inline onclick handler.

How do you check if the element is clicked?

To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked.


2 Answers

Very simple and effective one line solution without using jquery:

<a href="to/your/path" onclick="return confirm('Are you sure you want to delete?');">Delete</a>
like image 165
Indrasinh Bihola Avatar answered Oct 05 '22 00:10

Indrasinh Bihola


Try this.

$('.delete_row').click(function(){
    return confirm("Are you sure you want to delete?");
})
like image 32
ShankarSangoli Avatar answered Oct 04 '22 22:10

ShankarSangoli