shman
shman

Reputation: 3

why this javascript function is not executing, no error in console

This function is a problem:

function addInvoiceItemValue(name,pkwiu,netto,unit,qty,vat) {

if(vat == '23') v23 = " selected='selected'";
if(vat == '22') v22 = " selected='selected'";
if(vat == '8') v8 = " selected='selected'";
if(vat == '7') v7 = " selected='selected'";
if(vat == '5') v5 = " selected='selected'";
if(vat == '3') v3 = " selected='selected'";
if(vat == '0') v0 = " selected='selected'";
if(vat == 'zw') vzw = " selected='selected'";

var vatSelect = "<option value='23'"+v23+">23%</option><option value='22'"+v22+">22%</option><option value='8'"+v8+">8%</option><option value='7'"+v7+">7%</option><option value='5'"+v5+">5%</option><option value='3'"+v3+">3%</option><option value='0'"+v0+">0%</option><option value='zw'"+vzw+">zw.</option>";
var row = "<tr id='item" + itemID + "'><td><input size='30' maxlength='300' id='ii-name-" + itemID + "' name='ii-name-" + itemID + "' value='" + name + "'></td>";
row += "<td><input size='6'  maxlength='50' id='ii-pkwiu-" + itemID + "' name='ii-pkwiu-" + itemID + "' value='"+pkwiu+"'></td>";
row += "<td><input size='6'  maxlength='16' id='ii-netto-" + itemID + "' name='ii-netto-" + itemID + "' value='"+netto+"'></td>";
row += "<td><input size='5'  maxlength='128' id='ii-unit-" + itemID + "' name='ii-unit-" + itemID + "' value='"+unit+"'></td>";
row += "<td><input size='5'  maxlength='6' id='ii-qty-" + itemID + "' name='ii-qty-" + itemID + "' value='"+qty+"'></td>";
row += "<td><select id='ii-vat-" + itemID + "' name='ii-vat-" + itemID + "'>" + vatSelect + "</select></td>";
row += "<td><a onclick='delInvoiceItem(\"item" + itemID + "\")'><b>-</b> Usuń</a></td></tr>";

$('#invoiceItems tr:last').after(row);

itemID++;
}

Example execution:

addInvoiceItemValue('yyy','','676.76','','1','23');
addInvoiceItemValue('fgh','','777.00','','1','8');

And here is function that work's fine:

function addInvoiceItem() {

var vatSelect = "<option value='23'>23%</option><option value='22'>22%</option><option value='8'>8%</option><option value='7'>7%</option><option value='5'>5%</option><option value='3'>3%</option><option value='0'>0%</option><option value='zw'>zw.</option>";
var row = "<tr id='item" + itemID + "'><td><input size='30' maxlength='300' id='ii-name-" + itemID + "' name='ii-name-" + itemID + "' value=''></td>";
row += "<td><input size='6'  maxlength='50' id='ii-pkwiu-" + itemID + "' name='ii-pkwiu-" + itemID + "' value=''></td>";
row += "<td><input size='6'  maxlength='16' id='ii-netto-" + itemID + "' name='ii-netto-" + itemID + "' value='0'></td>";
row += "<td><input size='5'  maxlength='128' id='ii-unit-" + itemID + "' name='ii-unit-" + itemID + "' value=''></td>";
row += "<td><input size='5'  maxlength='6' id='ii-qty-" + itemID + "' name='ii-qty-" + itemID + "' value='1'></td>";
row += "<td><select id='ii-vat-" + itemID + "' name='ii-vat-" + itemID + "'>" + vatSelect + "</select></td>";
row += "<td><a onclick='delInvoiceItem(\"item" + itemID + "\")'><b>-</b> Usuń</a></td></tr>";

$('#invoiceItems tr:last').after(row);

itemID++;
}

Upvotes: 0

Views: 1061

Answers (3)

Scott
Scott

Reputation: 887

You should also check your selector.

$('#invoiceItems tr:last').after(row);

Try adding it in a simpler place. Make another div,...

Then use the selector $('#result');

In any case, if your selector is bad, it will never execute and it won't throw an error b/c it never had cause to do what you told it. Now that I think about it, I think this is the issue.

Do me a favor and try

$('#invoiceItems tr:last').each(alert('exist???'));

If it doesn't alert, your selector is most likely not working. (For each thing that meets the criteria , do an alert)

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196236

The v23, v22, v8, v7, v5, v3, v0, vzw and itemdID are not always defined in all code paths.

This causes the script to fail.


You should change your function to

function addInvoiceItemValue(name,pkwiu,netto,unit,qty,vat) {
    var vats = ['23','22','8','7','5','3','0','zw'];

    var vatSelect = '';
    for (var i = 0; i < vats.length; i++)
    {
        vatSelect += '<option value="'+vats[i]+'"';
        if (vat == vats[i])
            vatSelect += ' selected="selected"';
        vatSelect += '>'+vats[i] + '%</option>';
    }

    var row = "<tr id='item" + itemID + "'><td><input size='30' maxlength='300' id='ii-name-" + itemID + "' name='ii-name-" + itemID + "' value='" + name + "'></td>";
    row += "<td><input size='6'  maxlength='50' id='ii-pkwiu-" + itemID + "' name='ii-pkwiu-" + itemID + "' value='"+pkwiu+"'></td>";
    row += "<td><input size='6'  maxlength='16' id='ii-netto-" + itemID + "' name='ii-netto-" + itemID + "' value='"+netto+"'></td>";
    row += "<td><input size='5'  maxlength='128' id='ii-unit-" + itemID + "' name='ii-unit-" + itemID + "' value='"+unit+"'></td>";
    row += "<td><input size='5'  maxlength='6' id='ii-qty-" + itemID + "' name='ii-qty-" + itemID + "' value='"+qty+"'></td>";
    row += "<td><select id='ii-vat-" + itemID + "' name='ii-vat-" + itemID + "'>" + vatSelect + "</select></td>";
    row += "<td><a onclick='delInvoiceItem(\"item" + itemID + "\")'><b>-</b> Usuń</a></td></tr>";

    $('#invoiceItems tr:last').after(row);

    itemID++;
}

but the itemID also has to be defined.

Upvotes: 3

Scott
Scott

Reputation: 887

When is it SUPPOSED to be executing?

While this technically bad coding practice, test with this:

 <a href='#'  onClick="javascript: function('value');" > Click Me </a>

If you click, and it still doesn't work, it is an issue with your function. If you click and it works, the function is never being called in the first place.

Upvotes: 0

Related Questions