Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: button click confirmation

Tags:

jquery

I have the following html and jquery codes for deleting a member account with confirmation. But the code doesn't seem to work: even if I click "cancel" in the pop-up window, the request is still submitted and the account is deleted. How should I correct this? Thanks.

HTML:

<button class="member" href="/admin?arg1=deleteMember" onclick="return confirm('Are you sure?')">Delete member</button>

jQuery:

$(document).ready(function() {
  $('.member').click(function() {
    var url = $(this).attr('href');
    $('#content').load(url);
      return false;
  });
});
like image 490
Randy Tang Avatar asked Feb 16 '13 05:02

Randy Tang


1 Answers

$(document).ready(function() {
  $('.member').click(function() {
    if (confirm('Are you sure?')) {
      var url = $(this).attr('href');
      $('#content').load(url);
    }
  });
});

Then remove onclick attribute from the HTML element.

like image 142
Dane Hillard Avatar answered Sep 28 '22 02:09

Dane Hillard