amidou kante amidou kante - 1 year ago 55
HTML Question

Run javascript on multiple selected checkboxes

Hello I am currently writing a web application that calculates a number based on what a user has checked.

You can see it here.

If you go to the link you can see that a person will check a checkbox first and enter a value from the dropdown and type 2 values to get an output.

What I need help with is being able to calculate the value for more than one checkbox at a time.

Right now I can only calculate the value for a

one at a time even if multiple are selected. So basically I need help try to figure how to calculate for more than one
at a time.

I was using an
statement inside my javascript file but thats not giving me the result that I want.

Answer Source

Just remove else if statements, change for IF only. But I have to say that's solution its not that scalable, and for a big application could turns into a nightmare.

I recommend you try to use $("input:checked").each(function() {}); if you dont want to store it and just show the values on the client to avoid further problems and bad code. With your line of thought, you'll need everytime make copy and paste those IF's everytime you want to implement new form labels.

    function check(){
            var pokemonCount = parseInt($("#pokecount1").val());
            var candyCount = parseInt($("#candycount1").val());
            var reqCandy  =  parseInt($("#dropdown1 :selected").val());
            var evolveAmount = 0;

            evolveAmount =  Math.floor(((candyCount + pokemonCount) - 1) / (reqCandy));
            var pokemonCount2 = parseInt($("#pokecount2").val());
            var candyCount2 = parseInt($("#candycount2").val());
            var reqCandy2  =  parseInt($("#dropdown2 :selected").val());
            var evolveAmount2 = 0;

            evolveAmount2 = Math.floor(((candyCount2 + pokemonCount2) - 1) / (reqCandy2));
       if ($('#checkbox3').is(':checked')){
            var pokemonCount3 = parseInt($("#pokecount3").val());
            var candyCount3 = parseInt($("#candycount3").val());
            var reqCandy3  =  parseInt($("#dropdown3 :selected").val());
            var evolveAmount3 = 0;

            evolveAmount3 = Math.floor(((candyCount3 + pokemonCount3) - 1) / (reqCandy3));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download