1

I got a jquery code to create dynamic textbox. Here's my jquery code:

    var rowNum = 0;
    function addRow(frm) {
    rowNum ++;
    var row = '<p id="rowNum'+rowNum+'">Tanggal: <input type="text" class="datepick" name="qty[]" id="date'+rowNum+'" value="'+frm.add_qty.value+'" readonly="readonly"> Harga: <input type="text" name="name[]" value="'+frm.add_name.value+'" readonly="readonly"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
    jQuery('#itemRows').append(row);
    frm.add_qty.value = '';
    frm.add_name.value = '';
    }

    function removeRow(rnum) {
    jQuery('#rowNum'+rnum).remove();
    }

and here's my form code:

   <div class="form-group">
   <label>Price Given</label>
      <div id="itemRows">                                
      <label class="strong">Date: <input type="text" name="add_qty" class="datepick" id="date[]" /> 
      Price: <input type="text" name="add_name" /> 
        <input onclick="addRow(this.form);" type="button" value="Add" />
     </div>
    </div>
<div class="form-group">
    <label>Total</label>
      <input type="text" class="form-control" name="total" id="total" />
    <button type="button" name="calculate">Calculate</button>
</div>

My problem is, how to create my textbox 'total' can automatically or can calculate (by pressing button calculate) the total of textbox 'add_name' (price)? I hope there's an easiest/simplest way to do this calculate.

4
  • Name of the input filed which hods the value is "add_name", right? Commented Jul 9, 2015 at 3:21
  • Yes, correct! got the code from stackoverflow Commented Jul 9, 2015 at 3:30
  • Please Verify my answer. Check if the answer solves your problem Commented Jul 9, 2015 at 3:31
  • I have updated the "DEMO". Please verify. As you enter the value the total will change :) Commented Jul 9, 2015 at 3:54

2 Answers 2

1

Please try this code

 $(document).ready(function(){
        $("input[type='text'][name='add_name']").change(function(){
            var sum = 0
            $("input[type='text'][name='add_name']").each(function(){
                sum = sum + parseInt($(this).val());
            });
            $("input[type='text'][name='total']").val(sum);
        });
    });

DEMO

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

2 Comments

Hello, thanks for answer, i already tried that being this: jsfiddle.net/9dx1cxu8/5 but it's not automatically change the total value. or this method is used for the button? Sorry, I have minim knowledge at jquery
Its always pleasure to help others :)
0

try this one.

$('[name="calculate"]').on('click', function() {
    var sum = 0;
    $('[name="add_name"]').each(function() {
        sum += $(this).val();
    })
    $('#total').val(sum);
})

Comments

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.