Kieran Senior
Kieran Senior

Reputation: 18220

JSON Delete Table Row

I'm trying to delete a row of data using JSON, however when I prompt a confirm dialog my javascript function doesn't work as follows:

<script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
        $.ajax({
          type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
          success: function(msg) {
            if (msg.status == "ok") {
              $("tr#" + msg.id).hide();
            }
            else {
              alert(msg.exception);
            }
          }
        });

        return false;
      });
    });
</script>

The above works absolutely fine, but the minute I put the following in:

  <script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
        if (!confirm("Are you sure you want to delete this?")) return false;
        $.ajax({
          type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
          success: function(msg) {
            if (msg.status == "ok") {
              $("tr#" + msg.id).hide();
            }
            else {
              alert(msg.exception);
            }
          }
        });

        return false;
      });
    });
  </script>

This does carry out the delete, but it doesn't hide the table row, which makes me think it hasn't been deleted. Any ideas?

Upvotes: 0

Views: 2246

Answers (1)

ekhaled
ekhaled

Reputation: 2930

Try this:

  <script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
       if (confirm("Are you sure you want to delete this?")){ 
          $.ajax({
            type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
            success: function(msg) {
              if (msg.status == "ok") {
                $("tr#" + msg.id).hide();
              }
              else {
                alert(msg.exception);
              }
            }
           });
        }

        return false;
      });
    });
  </script>

hope it works for you...

Upvotes: 1

Related Questions