i am in mobile app and i want to fill up an array with values from checkboxes.
my code is
if (row.flatdescription == flatdescription) {
if (row.receiptno == 0){
items.push('<input type="checkbox" name="code_'+ i +'" id="code_'+ i +'" value="' + row.amount + '" previous="' + row.pastpayments + '" barcode="' + row.barcode + '" todayp="' + row.todaypayments + '"/><label for="code_'+ i +'">' + row.period +'..........'+ row.amount+'</label></br>');
}
allbarcode[i] = row.barcode;
previouspayments1 = previouspayments1 + row.pastpayments;
previouspayments = previouspayments1.toFixed(2);
sofeilon1 = sofeilon1 + row.amount;
sofeilon = sofeilon1.toFixed(2);
total1 = total1 + row.amount - row.pastpayments;
total = total1.toFixed(2);
}
and my array code
function barcodeTotal() {
barcode = [];
barcodeamount = [];
barcodeprevious = [];
$("input:checked").each(function(i) {
barcode[i] = $(this).attr("barcode");
barcodeamount[i] = $(this).attr("value");
barcodeprevious[i] = $(this).attr("previous");
});
}
my goal is to fill barcode array like this
barcode [barcode: barcode, amount: value, previous: previous, ....
i will appreciate your answers
You can transform your checked input fields into an array easily:
Now, depending on the desired result you may either harvest the values separately
or, what might be even better, as objects just like twilson suggested
In this case you want the
thisto be taken from inside of thefunctioncall,as it refers to the object, you matched with the
$('input:checked')call.If you store
var self = thisas twilson suggested, you would not receive the actual values of the input box, but no values, as thethisof the calling context is most likely not anHTMLElementat all.