2
\$\begingroup\$

It's been years since I've touched javascript and the strange scoping trips me up each time. Please could you point me into the right direction of how to both tidy this up and prevent the duplication?

It is simply two click events, with the only difference being the color updated on success.

$(function () {
 $(".manualSubmitComplete")
 .click(function () {
 $(".spinner").show();
 var $this = $(this);
 $.get(this.href)
 .done(function () {
 var $parent = $this.parent();
 $parent.children("spinner").show();
 $parent.children("a").remove();
 $parent.children("span").remove();
 $parent.attr("class", "");
 $parent.css("background-color", "#B0FFB0");
 $(".spinner").hide();
 });
 return false;
 });
 $(".manualSubmitReject")
 .click(function () {
 $(".spinner").show();
 var $this = $(this);
 $.get(this.href)
 .done(function () {
 var $parent = $this.parent();
 $parent.children("spinner").show();
 $parent.children("a").remove();
 $parent.children("span").remove();
 $parent.attr("class", "");
 $parent.css("background-color", "#FF0000");
 $(".spinner").hide();
 });
 return false;
 });
})
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Feb 14, 2017 at 15:52
\$\endgroup\$
1
  • \$\begingroup\$ Could you provide some context for these two click handlers? Why do the two classes manualSubmitComplete and manualSubmitReject exist in the first place? I suspect that you may be doing something wrong, but I can't tell for sure from just this excerpt. A demo would be helpful; you can create on by pressing Ctrl-M in the question editor. \$\endgroup\$ Commented Feb 14, 2017 at 21:53

1 Answer 1

3
\$\begingroup\$

You can simply extract the common code into a function. Arguably you could pass this rather than href and $target and derive ref and target in the function or pass $parent rather than $target, but the current arguments seem slightly clearer.

$(function() {
 var fetchUrl = function (href, $target, color) {
 $(".spinner").show();
 $.get(href)
 .done(function() {
 var $parent = $target.parent();
 $parent.children("spinner").show();
 $parent.children("a").remove();
 $parent.children("span").remove();
 $parent.attr("class", "");
 $parent.css("background-color", color);
 $(".spinner").hide();
 });
 };
 $(".manualSubmitComplete")
 .click(function() {
 fetchUrl(this.href, $(this), "#B0FFB0")
 return false;
 });
 $(".manualSubmitReject")
 .click(function() {
 fetchUrl(this.href, $(this), "#FF0000")
 return false;
 });
});
answered Feb 14, 2017 at 16:41
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.