0

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();
      });
    });
1

2 Answers 2

2

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/

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

3 Comments

You don't need each to add an event for them, I will update my post.
$(function () { //trigger calculate numbers and sum event $(".mad",".mad2").each(function() { $(this).keyup(function() { calculateSum(); }); });
Check it now and try that. That code you sent above is wrong also. It shouldn't be 2 strings... Just ".mad, .mad2" not ".mad", ".mad2"
1

You should go through multiple selector. Try this:

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

Comments

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.