s.d2614 s.d2614 - 2 months ago 6
jQuery Question

How to display all values get by jquery .each() and display in single alert box?

I would like to display a alert message based on some validation on checked items. Here is what I tried so far:

var result = [];
jQuery('#ct1').on("click", function(event){
jQuery(':checkbox:checked').each(function (i) {
qtyval = jQuery(this).parent().parent().siblings('.item-cell- detail').find('input[name="qty"]').val();
var qtyval1 = jQuery(this).parent().parent().siblings('.item-cell- detail').find('.qtyonhand').text();
var ndcCode = jQuery(this).parent().parent().siblings('.item-cell- detail').find('.ndc').text();
var itemName = jQuery(this).parent().parent().siblings('.item-cell- detail').find('.itemname').text();
var qtyu= parseFloat(qtyval1);
var strrsult = result.push(qtyval1);

if (qtyval > qtyu) {
var strrsult = result.toString();
alert(strrsult);
}
});
});


and the value of available quantity which i am getting is not displaying in single alert box.
As i have just started learning jQuery i am not getting how to display alert message in this format

Alert message format should be like this


"For the following items, the ordered quantity exceeds the current available quantity. Please adjust the quantity and retry.

NDCCODE1 Item Name1 - Available Qty: qty1
NDCCODE2 Item Name2 - Available Qty: qty2

Answer

Try to use it on a variable like this

jQuery('#ct1').on("click", function(event){
var txt = "";
var result = [];
jQuery(':checkbox:checked').each(function (i) {        
    qtyval = jQuery(this).parent().parent().siblings('.item-cell-    detail').find('input[name="qty"]').val();                                             
    var qtyval1 = jQuery(this).parent().parent().siblings('.item-cell-    detail').find('.qtyonhand').text();
    var ndcCode = jQuery(this).parent().parent().siblings('.item-cell-    detail').find('.ndc').text(); 
    var itemName = jQuery(this).parent().parent().siblings('.item-cell-    detail').find('.itemname').text(); 
    var qtyu= parseFloat(qtyval1);
    var strrsult = result.push(qtyval1);

    if (qtyval > qtyu) {
        var strrsult = result.toString(); 
        txt += strrsult + "\\n";
    }
});
alert(txt);
});

\n is for make a line break if dosn't work try with br