javascript confirm function return false

I want to show a message which writing are you sure … I can show the message but user click NO return false; did not work

<script>
function askCar(){
  var answer = confirm ("Are you sure for deleting car ?");
  if (answer){

  }
  else{
    return false;
  }
}
</script>

I call function with onclick here

echo '<td><a onclick="askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';

Upvotes: 3

Views: 8438

Answers (4)

ursitesion
ursitesion

Reputation: 988

When you use return within your function definition, you must have to use return in the place where you are calling that. Otherwise, you will not get proper result. So, the best code would be below

echo '<td><a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a><a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a></td>';

Upvotes: 0

ajtrichards
ajtrichards

Reputation: 30565

Your onclick function needs to use the return value:

echo '<td>
         <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">
            Sil
         </a>
         <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">
             Düzenle
         </a>
      </td>';

Upvotes: 1

Code Lღver
Code Lღver

Reputation: 15603

You need to add the return also:

echo '<td>
    <a onclick="return askCar()" href="arac-sil.php?sid='.$data["Car_ID"].'" class="ico del">Sil</a>
    <a href="arac-duzenle.php?did='.$data["Car_ID"].'" class="ico edit">Düzenle</a>
    </td>';

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

You need to use the return value:

<a onclick="return askCar();" ... >

Just having it inside the onclick will execute the function but to actually cancel the click, you need to use the return value.

Also, the code can be slightly optimized:

function askCar(){
    return confirm ("Are you sure for deleting car ?");
}

No need in if..else when you can directly return the dialog result. (unless of course you want to perform exra actions)

Upvotes: 11

Related Questions