I have to attach a jQuery to an invoice. Sometimes I have to print multiple invoices as once in a batch. When this happens my exact same jQuery appears for every invoice and
Change your repeating template to include a check:
<script>
if (typeof alreadyDone === "undefined") {
var alreadyDone = true;
$(document).ready(function() {
$('td.barcode_needed').append('<div class="bcTarget">');
$('.bcTarget').each(function() {
var $this = $(this);
$this.barcode('G' + $this.closest('td.barcode_needed').text(),'code128');
});
});
}
</script>
Now it should run only once.
Another thing to try, where we remove the classes as we go...
<script>
$('td.barcode_needed').each(function() {
$(this).append('<div class="bcTarget">');
});
$('.bcTarget').each(function() {
$(this).barcode('G' + $(this).closest('td.barcode_needed').text(),'code128');
$(this).removeClass('bcTarget');
$(this).closest('td.barcode_needed').removeClass('barcode_needed');
});
</script>