samedi 20 mars 2021

Jquery - How do you get checkbox values from closest parent only?

I am working on a school assignment and we are starting to learn Jquery

I am looking to collect the values from a set of check boxes but only the ones that fall within the div that the button that triggers the function is contained within.

So far I have been able to get the values, but if any of the boxes are checked in the other divs those values are added as well because they need to all share the same name. I am trying to avoid duplicating code.

This is my Jquery code:

$(document).ready(function() {
    $('button').on('click', function() {
        var destination = $(this).closest('.destinations'); //this selects the div the button is in but I can't figure out how to apply it to the checkbox value gathering
        var base_price = $(destination).find('.base_price').text();
        var add_ons = [];
        $.each($("input[name='add_on']:checked"), function(){ //I player around with using the variable destination somewhere here but am thinking there needs to be another step
            add_ons.push($(this).val());
        });
        $(this).remove();
        console.log(base_price)
        console.dir(add_ons) //the values are successfully added but I can't figure out how to isolate based on the the variable 'destination'
        });
    });

Any assistance will be greatly appreciated




Aucun commentaire:

Enregistrer un commentaire