0

Hi guys i'm currently working on a delete validate function in javascript. Here is my code.

  function confirmDelete(){
var agree = confirm("Are you sure you want to delete this file?");
  if(agree == true){
    return true
}
else{
return false;
}
}

The alert dialog box comes out but whenever i press "Cancel" button it still deletes the file in the table

Here is my HTML:

<a href=".base_url()."main/delete?id=$row->id>"."<i class='icon-trash' rel = 'tooltip' title = 'Delete' id = 'delete' onClick = 'confirmDelete();'></i></a>";

I'm using CodeIgniter by the way any help will much be appreciated! Thanks guys! :)

IamRey
  • 3
  • 1
  • 4

4 Answers4

1

Because the on click should be in the <a>, not the <i>. Also, your function could be simplified, considering confirm returns true or false:

function confirmDelete(){
    return confirm("Are you sure you want to delete this file?");
}
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • It still does the same :( – IamRey Aug 01 '13 at 02:39
  • Could you show the whole PHP line from the original code? Or just the HTML output? You posted something in between, which is hard to read. – bfavaretto Aug 01 '13 at 02:46
  • public function delete(){ $id = $this->input->get('id'); $delete_row = array("id" => "$id"); $this->load->model('user_model'); $this->user_model->delete_user($delete_row); $this->view_emp(); } Here is my delete function in my controller do you think I can put the validation in my controller? – IamRey Aug 01 '13 at 02:51
  • @thisshit No, I mean the same you posted in the question, but complete, with echo, the outer quotes, etc. your quotes look suspect. – bfavaretto Aug 01 '13 at 03:02
  • @thisshit Nevermind, JSHunjan is right, I missed that. The tip about simplifying your function stands, though. – bfavaretto Aug 01 '13 at 03:45
  • Thank you for your efforts anyway sir! Greatly appreciated :) – IamRey Aug 01 '13 at 03:48
1

Just do it directly inside your tag:

   <a href=".base_url()."main/delete?id=$row->id>"  OnClick="return confirm('Are you sure you want to delete this book?');"> Delete </a>
leonardeveloper
  • 1,813
  • 1
  • 34
  • 58
1

try to use return statement where you are calling javascript function. Like below:

<a href=".base_url()."main/delete?id=$row->id>"."<i class='icon-trash' rel = 'tooltip'   title = 'Delete' id = 'delete' onClick = 'return confirmDelete();'></i></a>";
JSHunjan
  • 397
  • 1
  • 5
  • 18
0

Call event.preventDefault(), if returning false on its own is not preventing the browser action.

You will need to get a reference to event or window.event.. which is somewhat browser-specific. jQuery or similar can help with this.

onclick="if (! confirm('Are you sure you want to delete this book?')) event.preventDefault();"

(Though, with the above example, we're not returning 'false' any more -- ideally, we'd put the code in a method, and both return false and prevent default if the confirmation is negative.)

To test if it works, most simply:

onclick="event.preventDefault();"

See also:

The difference between returning 'false' and e.preventDefault() will prevent the default event from occuring,e.stopPropagation()` prevents it from bubbling up/ outwards & outer defaults occurring, but return 'false' does not stop it from bubbling.

So perhaps it would be better to put the onclick handler on the link, which is performing the default action?

Community
  • 1
  • 1
Thomas W
  • 13,940
  • 4
  • 58
  • 76