Kavvson Empcraft
Kavvson Empcraft

Reputation: 443

Select2 - Get data from a loop

I have a form-table and each row contains one select2, I want to get data from each of these and store it to array. Below is my code I would use, not sure if its proper attempt.

// event on each form change re-get the values
         $("#main").bind("keyup change", function (e) {


                        $('#form > tbody  > tr').each(function (i) {

        // get the data for each select
        // add it to array and use after loop for some logic
        // not sure if need this event here : select2:select
        // also the selector might be not in the node of current tr?

                            $('.selector').on("select2:select", function (e) {

                                var data = $(this).select2('data');

                               // push to array ?? data[0].custom_data

                            });

                        });

    // logic from array would be here

                    });

Upvotes: 1

Views: 2209

Answers (1)

Aparajit P Utpat
Aparajit P Utpat

Reputation: 385

You must find the required element in the tr

Replace

$('.selector').on("select2:select", function (e) {

                            var data = $(this).select2('data');

                           // push to array ?? data[0].custom_data

                        });

with

var data = $(this).find('.selector').select2('data');

Upvotes: 4

Related Questions