0

I've been trying to update results based off of how many checkboxes a user has checked on a page.

If the user has checked 5 or more then it should fade 2 divs.

If the user has checked 10 or more it should fade another two divs.

If the user chooses 10 or more it should fade another set.

I can get the first function to run but I believe since the first if statement has been completed, it's not looking for updates/value changes? The others do not fade back to 100% opacity either.

$('input:checkbox').change(function() {
    var boxChecked = $('input:checkbox:checked').length;
    if (boxChecked > 5) {
        $('.div-1, .div-2').fadeTo(500, 0.4)
    }
    else if (boxChecked > 10) {
        $('.div-1, .div-3').fadeTo(500, 0.4)
    }
    else if (boxChecked > 11) {
        $('.div-1, .div-2').fadeTo(500, 0.4)
    }
});
7
  • Are the divs supposed to cumulatively fade, or is it only for the greatest condition that is met? Commented Aug 27, 2014 at 19:12
  • The div-3 and the div-2 in the 2nd and 3rd fadeTo statements, respectively, are both missing the . class selector. Is this intended? Commented Aug 27, 2014 at 19:12
  • plus you want to use boxChecked >= 5 style for all your ifs, since you said "if the user has checked 5 or more". boxChecked > 5 means the code won't be run unless the user has 6 or more boxes checked. Commented Aug 27, 2014 at 19:14
  • Sorry, updated the class selectors. Not sure why they were missing but were there in my js file. Commented Aug 27, 2014 at 19:14
  • 1
    @AoiHana both 5 and 11 are greater than 10, what should happen when 11 are checked? Should div-1, div-2, and div-3 fade out? or just 1 and 3 Commented Aug 27, 2014 at 19:22

2 Answers 2

1

You need to either set ranges for your if/else statement conditions or just use if statements for your code to function correctly.

For example, if length > 5, the first branch will be taken.

However, your second branch checks if length > 10, but will never reach this statement since the first condition is also true.

You can try something like this:

   $('input:checkbox').change(function() {
    var boxChecked = $('input:checkbox:checked').length;
    if (boxChecked === 2) {
        $('.div-1, .div-2').fadeTo(500, 0.4);
    }
    else if (boxChecked > 3 && boxChecked <5) {
        $('.div-1, .div-3').fadeTo(500, 0.4);
    }
    else if (boxChecked > 4) {
        $('.div-1, .div-2').fadeTo(500, 0.4);
    }
    else {
        $('.div-1, .div-2, .div-3').fadeTo(500,1);
    }
});

Edit: Added a jsfiddle and fixed a bug - added an else to reset the divs when conditions aren't met. JSFiddle: http://jsfiddle.net/148cL5rc/1/

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

1 Comment

Thank you for your time. This helped me a lot. I understand better now how to set these up.
1

Reverse your if statements. > 5 is always going to be true when you get above 5, so you want to start checking with the higher numbers and work your way to lower numbers.

$('input:checkbox').change(function() {
    var boxChecked = $('input:checkbox:checked').length;
    if (boxChecked > 11) {
        $('.div-1, .div-2').fadeTo(500, 0.4)
    } else if (boxChecked > 10) {
        $('.div-1, .div-3').fadeTo(500, 0.4)
    } else if (boxChecked > 5) {
        $('.div-1, .div-2').fadeTo(500, 0.4)
    }
});

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.