0

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++;
}
4
  • And your question is? And the errors are? Commented Jan 12, 2011 at 20:17
  • "no error in console", first function is not executing. Why? What is wrong? Commented Jan 12, 2011 at 20:19
  • where are the "v-variables" coming from? (v23,v22,v8, etc.) Commented Jan 12, 2011 at 20:20
  • still, only one gets defined so all other uses fail.. Commented Jan 12, 2011 at 20:26

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

v23, v22, v8, v7, v5, v3, v0, vzw are defined inside function and itemdID is definded outside function.
if(vat == '23') v23 = " selected='selected'"; What happens if vat doesn't equal '23'?
@shman, updated answer to include a workaround that automates the creation of the options..
0

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.

4 Comments

It's loaded with page part (jQuery.load())
did you insert alert('test') in the first line? In general, you can alert your way through something as if you had a console view....
yep, your comment helped to debug this problem. Solution:
if(vat == '23') { v23 = " selected='selected'"; } else { v23 = ""; } if(vat == '22') { v22 = " selected='selected'"; } else { v22 = ""; } if(vat == '8') { v8 = " selected='selected'"; } else { v8 = ""; } if(vat == '7') { v7 = " selected='selected'"; } else { v7 = ""; } if(vat == '5') { v5 = " selected='selected'"; } else { v5 = ""; } if(vat == '3') { v3 = " selected='selected'"; } else { v3 = ""; } if(vat == '0') { v0 = " selected='selected'"; } else { v0 = ""; } if(vat == 'zw') { vzw = " selected='selected'"; } else { vzw = ""; }
0

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)

1 Comment

Cool. So in the end, that was not the problem. I know these are just small little things that kind of apply in generic trouble shooting. Hard to compete with the guy who rewrites the function! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.