vendredi 17 juin 2016

jQuery checkbox value issue

I'm quite new to jQuery and I am trying to create a checkbox form, where you can add an element x to the list A (id=2), list B(id=3) and/or list C(id=77). The IDs are just an example for database purpose. So I created a form listing A, B and C. I gave each checkbox the value of the lists id.

Then I target the checkbox with

$("[id^=myCheckbox]").change(..)

when it is clicked and grab the id (of the list) via this.val().

My problem is that I only know the ID of the list which was clicked. How can I determine the value of the element x? I would like to have multiple elements, let's say x,y and z. I want to list for each element a checkbox form where you can select the list A, B and/or C.

    $("[id^=myCheckbox]").change(function() {

        var elementID = ????

        // selected list ID
        var listID = $(this).val();

        // determine checkbox state
        if(this.checked) {
            // add element to list (with ajax)
  
        }
        else {
            // remove element from list

        }
    });

I would be glad if somebody can help me. Thank you very much




Aucun commentaire:

Enregistrer un commentaire