0

code for multiplication javascript that will multiply unitprice and quantity and will display the result to the another textbox(total)

<script>
function calculate() {
        var myunitprice = document.getElementById('unitprice').value;   
        var myquantity = document.getElementById('quantity').value;
        var result = document.getElementById('total');  
        var myResult = myunitprice * myquantity;
        result.value = myResult.toFixed(2); }
</script>

Here's the another code for dynamic textbox, when select it will add another textbox.

    <script>
var php = '';
$('#children').on('change', function() {
    children = $(this).val();
    html = '';
    for (var i = 0; i < children; i++) {
        html += '<table><tr><td><label><b>Item Purchase</b></label><input type="text" id="item' + i + '" name="child' + i + 'Name" /><label>Amount</label><input type="text" id="amount' + i + '" name="child' + i + 'Name" /><label>Unit</label><input type="text" id="unit' + i + '" name="child' + i + 'Name" /><label>Quantity</label><input type="text" id="quantity' + i + '" name="child' + i + 'Name" onchange="calculate()" /><label>Unit Price</label><input type="text" name="child' + i + 'Age" id="unitprice' + i + '" onchange="calculate()" /><td>';
    }

    $('#kidsFields').html(html);
    $('#kidsFields').append('<br><td><input type="submit" value="Submit" /></td></tr></table>');

    $('.ui-page').trigger('create');
});
</script>

Here's my html

<form data-ajax="false" method="post" action="purchasingAddRecords.php">
 <center>
 <table>    
 <tr>
 <td>

 <td>

<div data-role="content">
<li data-role="fieldcontain"> 
<label for="children" class="select">Add</label>
<select name="children" id="children" data-mini="true">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select> 
</li>
<div id="kidsFields"></div>
</div>

the problem is that when i put data on the textbox(quantity and unitprice), it doesn't calculate.

0

4 Answers 4

2

You have several errors in your scripts:

  • You are adding a number next to the function call
  • You are not passing the number of iteration to the function
  • You don't have an element with id='total'

Try using these two scripts:

<script>
    function calculate(number) {
       var myunitprice = document.getElementById('unitprice'+ number).value;
       var myquantity = document.getElementById('quantity' + number).value;
       var result = document.getElementById('total' + number);
       var myResult = myunitprice * myquantity;
       result.value = myResult.toFixed(2);
    }

    $('#children').on('change', function() {
        children = $(this).val();
        injection = '';
        for (var i = 0; i < children; i++) {
            injection += '<table><tr><td><label><b>Item Purchase</b></label><input type="text" id="textName' + i + '" name="child' + i + 'Name" /><label>Amount</label><input type="text" id="textName' + i + '" name="child' + i + 'Name" /><label>Unit</label><input type="text" id="textName' + i + '" name="child' + i + 'Name" /><label>Quantity</label><input type="text" id="quantity' + i + '" name="child' + i + 'Name" oninput="calculate(' + i + ')" value="0" /><label>Unit Price</label><input type="text" name="child' + i + 'Age" id="unitprice' + i + '" oninput="calculate(' + i + ')" value="0" /><label>Total</label><input type="text" id="total' + i + '" value="0" /> <td>';
        }

        $('#kidsFields').html(injection);
        $('#kidsFields').append('<br><td><input type="submit" value="Submit" /></td></tr></table>');

        $('.ui-page').trigger('create');
    });
</script>

I have a working plunker for that http://plnkr.co/edit/iKuIgbROfSEic6BNTiPy?p=preview

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

1 Comment

Thank you EliJah it works like a charm. i will take note of that.
0

Change

var myResult = myunitprice * myquantity;

to

var myResult = parseFloat(myunitprice) * parseInt(myquantity);

2 Comments

it doesn't work, i think there's something wrong with my input text format script part.
Try alerting the variables myunitprice and myunitquantity and see what you get
0

try onchange instead of oninput

like this

oninput="calculate()' + i +'" to onchange="calculate()"

NOTE: onchange will run function when you lost focus from your textbox.

2 Comments

Hey, you haven't set your id properly though. Check your all textbox ids as per calculate().
yes disha, but i'm just trying to test first the unitprice and the quantity part if it's working.
0

Updated Code

var php = '';
$('#children').on('change', function () {
children = $(this).val();
html = '<table>';
for (var i = 0; i < children; i++) {
    html += '<tr><td><label>Quantity</label><input type="text" id="quantity' + i + '" name="child' + i + 'Name" oninput="calculate(' + i + ')" /></td><td><label>Unit Price</label><input type="text" name="child' + i + 'Age" id="unitprice' + i + '" oninput="calculate(' + i + ')" />' +
        '</td><td><label>Total</label><input readonly="readonly" type="text" name="total" id="total' + i + '"  /></td></tr>';
}
html += '</table><label>Grand Total </label><input type="text" id="GrandTotal" />'
$('#kidsFields').html(html);
$('.ui-page').trigger('create');
}); 

function calculate(i) {
    var myunitprice = document.getElementById('unitprice' + i).value;
    var myquantity = document.getElementById('quantity' + i).value;
    var result = document.getElementById('total' + i);
    var myResult = myunitprice * myquantity;
    result.value = myResult.toFixed(2);
    TotalValue();
}

function TotalValue() {
    var totalValue = 0;
    $(document).find('input[name=total]').each(function(index, Obj) {
        if (isNaN(parseFloat($(Obj).val()))) $(Obj).val(0)
        totalValue += parseFloat($(Obj).val())

    })
    $('#GrandTotal').val(totalValue.toFixed(2))
}

http://jsfiddle.net/santoshj/5796xu77/

3 Comments

thank you Santosh, but i only need 1 total textbox, i'll try to add all the total's of by adding them. thank you again.
this is what i wass looking for, but if you try 23 * 23 = 575.00 the result is wrong. it should be 529.00. i think there's something wrong with the formula.
updated fiddle and code . to add all totals you must store them in some place. i used input element , you are free to change it .

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.