mardi 11 juillet 2017

Adding sum of all the rels of the inputs. Works fine with the checkboxes, that is until select inputs are added

$(document).ready(function() {
        function recalculate() {
            var sum = 0;

            $("input[type=checkbox]:checked").each(function() {
                sum += parseInt($(this).attr("rel"));
            });

            $("#output").html(sum);
        }


        $("input[type=checkbox]").change(function() {
            recalculate();
        });
    });

Here is the javascript that gets the sum of the checkboxes. Now i'm not sure how to properly add select inputs into this function. I have tried just doing

$("input[type=select]:selected").each(function()

and adding it into the code but to no avail.

Explanation: So basically everything works fine with the check boxes. The rels are added each time a check box is selected. I just have been struggling to get the select input options rels to work with the checkbox sum function as well.

Any help is really appreciated! Thanks!

[EDIT]: SOLUTION ATTEMPT UNSUCCESSFUL

      $(document).ready(function() {
        function recalculate() {
            var sum = 0;

            $("input[type=checkbox]:checked").each(function() {
                sum += parseInt($(this).attr("rel"));
            });

            $( "select option:selected" ).each(function() {
                sum += parseInt($(this).attr("rel"));
            });

            $("#output").html(sum);
        }

        $("input[type=checkbox]").change(function() {
            recalculate();
        });

        $("select option:selected").change(function() {
            recalculate();
        }); 

Aucun commentaire:

Enregistrer un commentaire