Koala7
Koala7

Reputation: 1404

Remove Class on input cheched not working

I have set that if the Input checkbox is checked the opacity turns from 0.5 to 1.

It's not working, it actually works if i do the opposite, but this is not my goal!

My CSS code

    .opacitychange {opacity: 1;}
    #total {opacity: 0.5;}

and

    if($("#iva").is(':checked'))
    {
        $('#total').html('+' + vat);
        total += vat;
        $('#total').addClass("opacitychange");
    }    
    else
        $('#total').html('0.00').removeClass("opacitychange");

    if($("#irpef").is(':checked'))
    {
        $('#total1').html('-' + irpf);
        total -= irpf;
        $('#total1').addClass("opacitychange");
    }
    else
        $('#total1').html('0.00').removeClass("opacitychange");
        $("#total2").html(total.toFixed(2));
    };

What's wrong?

Here the case

Upvotes: 0

Views: 148

Answers (1)

Polmonite
Polmonite

Reputation: 943

If your code really is:

if($("#iva").is(':checked'))
{
    $('#total').html('+' + vat);
    total += vat;
    $('#total').addClass("opacitychange");
}    
else
    $('#total').html('0.00').removeClass("opacitychange");

if($("#irpef").is(':checked'))
{
    $('#total1').html('-' + irpf);
    total -= irpf;
    $('#total1').addClass("opacitychange");
}
else // <-- this is not a typo?
    $('#total1').html('0.00').removeClass("opacitychange");
    $("#total2").html(total.toFixed(2));
};

then you just need to add brackets {} to your else statement. You have the same problem in your jsFiddle, where your else statement is:

else
    $('#total').html('0.00');
    $('#total').html('0.00').removeClass("opacitychange");

You actually execute only the first instruction in the else statement; then always remove the class. After fixing that you still could have some problems with css gerarchy, but if you change:

.opacitychange {opacity: 1;}

to:

.opacitychange {opacity: 1 !important;}

then it should work.

PS: sorry for my bad english.

Upvotes: 2

Related Questions