Click a link to delete targeted content using Ajax
Clash 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.
- 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 ()
$("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?
javascript jquery ajax django
add a comment |Â
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.
- 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 ()
$("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?
javascript jquery ajax django
add a comment |Â
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.
- 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 ()
$("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?
javascript jquery ajax django
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.
- 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 ()
$("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?
javascript jquery ajax django
edited Jul 23 at 0:27
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jul 17 at 15:08
JawSaw
2276
2276
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
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...
add a comment |Â
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.
- 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...
add a comment |Â
up vote
1
down vote
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...
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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...
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...
answered Jul 25 at 0:19
Louys Patrice Bessette
1417
1417
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password