1

I have a section of jQuery code that I am having to repeat over and over with only a small change accompanying each iteration. Here is an example of what I have:

  if($('.smm').hasClass('vendor-icon-active')) {
      total = total + 1200;
  } else {
    total = total;
  }

  if($('.repman').hasClass('vendor-icon-active')) {
      total = total + 495;
  } else {
    total = total;
  }

  if($('.blog-management').hasClass('vendor-icon-active')) {
      total = total + 395;
  } else {
    total = total;
  }

  if($('.press-release').hasClass('vendor-icon-active')) {
      total = total + 195;
  } else {
    total = total;
  }

In my code, I have about 30 of those sections. Is there a way I could simplify that process and clean up my code?

4
  • 2
    the else is not needed, that would instantly reduce the size of your code quite a bit. Commented Oct 15, 2013 at 14:45
  • Oh really? So it work if I just kept the if statement? Commented Oct 15, 2013 at 14:46
  • Yup. The else literally does nothing in this case. Commented Oct 15, 2013 at 14:46
  • Yes because inside the else you are just setting total = total; Commented Oct 15, 2013 at 14:46

2 Answers 2

5

You can use a common class to group the elements, and a data-* attribute to hold the value associated with them. Try this:

var total = 0;
$('.item').each(function(i, el) {
  var $item = $(el);
  if ($item.hasClass('vendor-icon-active')) {
    total += +$item.data('value');
  }
});
console.log(total);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="smm vendor-icon-active item" data-value="1200">Foo</div>
<div class="repman vendor-icon-active item" data-value="495">Foo</div>
<div class="blog-management vendor-icon-active item" data-value="395">Foo</div>
<div class="press-release vendor-icon-active item" data-value="195">Foo</div>

Sign up to request clarification or add additional context in comments.

3 Comments

Oh yeah that actually looks great! is data-value supported by older versions of IE?
you could use 'vendor-icon-active' as the common class so you won't need the if hasClass
@AbrahamUribe that's true for my example, although it would depend on the OPs DOM structure.
2

This should do the trick...

function updateTotal(className, value) {
    if ($('.' + className).hasClass('vendor-icon-active')) {
        total += value;
    }
}

updateTotal("ssm", 1200);
updateTotal("repman", 495);
updateTotal("blog-management", 395);
updateTotal("press-release", 195);

I just moved the main functionality into that one function. You can add as many function calls as you need afterwards :)

1 Comment

I do prefer @RoryMcCrossan's answer, but this gives you options and 2 very different styles of getting the same result.

Your Answer

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.