Click a link to delete targeted content using Ajax I used a glyphicon as a link to delete a comment:
&lt;a class=&quot;delCommentLink&quot; href=&quot;{% url 'article:comment_delete' comment.id %}&quot;&gt;
    &lt;span id=&quot;{{ comment.id }}&quot; class=&quot;glyphicon glyphicon-trash&quot; aria-hidden=&quot;true&quot;&gt;delete&lt;/span&gt;
&lt;/a&gt;

I send the request using Ajax.

retrieve the comment_url
get the grand-grand-parent element to hide
send request to views.py and delete the target comment


$(document).ready(function () {
    $(&quot;body&quot;).on(&quot;click&quot;,&quot;.delCommentLink&quot;,function (e) {
        e.preventDefault();
        var comment_url = $(e.target).parent().attr(&quot;href&quot;);
        var $commentEle = $(e.target).closest(&quot;.comment&quot;);
        if (window.confirm(&quot;Delete this comment?&quot;)) {
            $.ajax({
                type: &quot;get&quot;,
                url: comment_url,
                success: function (data) {
                    var ret = JSON.parse(data);
                    if (ret['status'] == 1) {
                        $commentEle.hide();
                    } else {
                        alert(ret['msg']);
                };
            },//success
        });//ajax  
        } else {
            e.preventDefault()
        }
    });//click event
})

Click a link to delete it. My code seems like it has too many lines for such a routine task.
How can I complete it elegantly?
I only see two things to "reduce", regarding the line amount.


The if statement in the success callback could be replaced by a ternary operator.
The last e.preventDefault() is redundant, you can squarely remove the whole else part.

$(document).ready(function () {
  $("body").on("click",".delCommentLink",function (e) {
    e.preventDefault();
    var comment_url = $(e.target).parent().attr("href");
    var $commentEle = $(e.target).closest(".comment");
    if (window.confirm("Delete this comment?")) {
      $.ajax({
        type: "get",
        url: comment_url,
        success: function (data) {
          var ret = JSON.parse(data);
          (ret['status'] == 1) ? $commentEle.hide() : alert(ret['msg']);
        },//success
      });//ajax  
    }
  });//click event
});



That is 6 lines less...