Click a link to delete targeted content using Ajax

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I wrote a glypicon as a link to delete a comment:



<a class="delCommentLink" href="% url 'article:comment_delete' comment.id %">
<span id=" comment.id " class="glyphicon glyphicon-trash" aria-hidden="true">delete</span>
</a>


I send the request using Ajax.



  1. retrieve the comment_url

  2. get the grand-grand-parent element to hide

  3. send request to views.py and delete the target comment



$(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);
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?







share|improve this question



























    up vote
    0
    down vote

    favorite












    I wrote a glypicon as a link to delete a comment:



    <a class="delCommentLink" href="% url 'article:comment_delete' comment.id %">
    <span id=" comment.id " class="glyphicon glyphicon-trash" aria-hidden="true">delete</span>
    </a>


    I send the request using Ajax.



    1. retrieve the comment_url

    2. get the grand-grand-parent element to hide

    3. send request to views.py and delete the target comment



    $(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);
    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?







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I wrote a glypicon as a link to delete a comment:



      <a class="delCommentLink" href="% url 'article:comment_delete' comment.id %">
      <span id=" comment.id " class="glyphicon glyphicon-trash" aria-hidden="true">delete</span>
      </a>


      I send the request using Ajax.



      1. retrieve the comment_url

      2. get the grand-grand-parent element to hide

      3. send request to views.py and delete the target comment



      $(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);
      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?







      share|improve this question













      I wrote a glypicon as a link to delete a comment:



      <a class="delCommentLink" href="% url 'article:comment_delete' comment.id %">
      <span id=" comment.id " class="glyphicon glyphicon-trash" aria-hidden="true">delete</span>
      </a>


      I send the request using Ajax.



      1. retrieve the comment_url

      2. get the grand-grand-parent element to hide

      3. send request to views.py and delete the target comment



      $(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);
      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?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 23 at 0:27









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jul 17 at 15:08









      JawSaw

      2276




      2276




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I only see two things to "reduce", regarding the line amount.



          1. The if statement in the success callback could be replaced by a ternary operator.


          2. 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...






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199688%2fclick-a-link-to-delete-targeted-content-using-ajax%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I only see two things to "reduce", regarding the line amount.



            1. The if statement in the success callback could be replaced by a ternary operator.


            2. 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...






            share|improve this answer

























              up vote
              1
              down vote













              I only see two things to "reduce", regarding the line amount.



              1. The if statement in the success callback could be replaced by a ternary operator.


              2. 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...






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                I only see two things to "reduce", regarding the line amount.



                1. The if statement in the success callback could be replaced by a ternary operator.


                2. 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...






                share|improve this answer













                I only see two things to "reduce", regarding the line amount.



                1. The if statement in the success callback could be replaced by a ternary operator.


                2. 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...







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 25 at 0:19









                Louys Patrice Bessette

                1417




                1417






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199688%2fclick-a-link-to-delete-targeted-content-using-ajax%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?