Jesper Møller
Jesper Møller

Reputation: 137

trigger function on multiple events/input fields and calculate them?

I have this small functions that trigger the even to calculate the numbers and sum of input fields with class mad

$(".mad").each(function() {
  $(this).keyup(function() {
  calculateSum();
  });
});

However I have two types of input fields I need to calculate mad and mad2 so I was thinking if something like this would be possible to trigger the calculation on both mad and mad2 fields?

 $(".mad" && ".mad2").each(function() {
      $(this).keyup(function() {
      calculateSum();
      });
    });

Upvotes: 0

Views: 228

Answers (2)

Näbil Y
Näbil Y

Reputation: 1650

Simply, use this:

 $(".mad, .mad2")

Your code would become:

$(".mad, .mad2").keyup(function() {
   calculateSum();
});

jQuery are using CSS selectors, so basically you can do anything that CSS selectors can. http://api.jquery.com/category/selectors/

Upvotes: 2

Kiran
Kiran

Reputation: 20313

You should go through multiple selector. Try this:

$(".mad,.mad2").each(function() {
      $(this).keyup(function() {
      calculateSum();
      });
});

Upvotes: 1

Related Questions