Question

I am attempting to print out lables (bar codes) from a table using JS (the table is using JQ Tablesorter) and the barcode jquery. My issue is that I need to iterate through all of the isbn's and it is showing one number per line. Here is the code I have:

    $("#barcode").live('click', function(){
var title="";
var isbn="";
var first = "";
var second = "";
var indexGlobal = 0;

$('#acctRecords tbody tr').each(function()
{
    isbn += $(this).find('#tableISBN').html();
    title += $(this).find('#tableTitle').html();

    });  //end of acctRecords tbody function

//Print the bar codes

    var x=0;
    for (x=0;x<isbn.length;x++)
        {


        first += '$("#'+indexGlobal+'").barcode("'+isbn[x]+'", "codabar",{barHeight:40, fontSize:30, output:"bmp"});';
        second += '<div class="wrapper"><div id="'+indexGlobal+'"></div><div class="fullSKU">&nbsp &nbsp &nbsp '+isbn[x]+
        '</div><br/><div class="title">'+title[x]+'</div></div><br/><br/>';
        indexGlobal++;

        }
var barcode =  window.open('','BarcodeWindow','width=400');
        var html = '<html><head><title>Barcode</title><style type="text/css">'+
        '.page-break{display:block; page-break-before:always; }'+
        'body{width: 8.25in;-moz-column-count:2; -webkit-column-count:2;column-count:2;}'+
        '.wrapper{height: 2.5in;margin-left:10px;margin-top:5px;margin-right:5px;}'+
        '.fullSKU{float: left;}'+
        '.shortSKU{float: right;font-size:25px;font-weight:bold;}'+
        '.title{float: left;}'+
        '</style><script type="text/javascript"src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.js"></script><script type="text/javascript" src="../barcode/jquery-barcode.js"></script><script>$(document).ready(function() {'+first+'window.print();window.close();});</script></head><body>'+second+'</body></html>';
        barcode.document.open();
        barcode.document.write(html);
        barcode.document.close();

}); // end of click function

I am pretty sure that the issue is with these lines:

var x=0;
for (x=0;x<isbn.length;x++)

For example if an isbn is 9780596515898 I am getting 9 on the first line, 7 on the second, 8 on the third etc. How do I get it to print out the entire isbn on one line?

Was it helpful?

Solution

Nope, those 2 lines are fine. But these 2, on the other hand...

var isbn="";
...
isbn += $(this).find('#tableISBN').html();

This makes isbn a string. And you are just making the string longer every time you add an isbn to it. "string".length will tell you the number of characters in that string, which is why you get one character per iteration.

You want an array instead, which you append items to with the [].push() method. [].length will tell you the number of items in that array.

var isbn = [];
...
isbn.push($(this).find('#tableISBN').html());
for (var x=0; x<isbn.length; x++) {
  isbn[x]; // one isbn
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top