Reputation: 443
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
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