here i am trying to do calculation for javascript dynamic rows. i have done auto multiplication in few rows using this function . but i want to calculate the tax percentage. how can i do that? help..

<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script>
var $k = jQuery.noConflict();

$k.fn.sum = function() {
    var sum = 0;
    $k(this).each(function() {
        sum += parseFloat($k(this).val());
    });

    return sum;
}

$k(function() {
    $k('select[name^=noofnights], input[name^=unitprice]').live("keyup", function() {
        var noofnights = parseFloat($k(this).parent().find('select[name^=noofnights]').val());
        var unitprice = parseFloat($k(this).parent().find('input[name^=unitprice]').val());
        $k(this).siblings('input[name^=linetotal]').val(noofnights * unitprice);

        var sum = $k('input[name^=linetotal]').sum();

        $k('#subtotal').val(sum);
        $k('#tax').val(Math.round(sum * 10 * 100) / 100);
        $k('#total').val(Math.round((sum + Math.round(sum * 10 * 100) / 100) * 100) / 100);
    });

    $k('.add').live('click', function() {
        $k(this).closest('.item').after($k('#template').html());
        $k('form .item').each( function(i) {
           $k(this).find('input[name^=num]').val(i+1);
        });
    });
});
</script>



<div id="template">
    <div class="item">
        <input name="num" value="1" size="5">        
        <input name="desc" value="Description">
        <select name="noofnights">
        <option value="empty"></option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="30">30</option>
      <option value="31">31</option>
    </select>
        <input name="unitprice" value="0">
        <input name="linetotal" value="0">
        <input name="tax" value="0" />
    </div>
</div>

<form id="item_form">
    <div class="item">
        <input name="num" value="1" size="5">
        <input name="desc" value="Description One">
        <select name="noofnights" value="0">
        <option value="empty"></option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="30">30</option>
      <option value="31">31</option>
    </select>
        <input name="unitprice" value="0">
        <input name="linetotal" value="0">
        <input name="tax" value="0" />
    </div>
    <div class="item">
        <input name="num" value="2" size="5">
        <input name="desc" value="Description Two">
        <select name="noofnights" value="0">
        <option value="empty"></option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
      <option value="13">13</option>
      <option value="14">14</option>
      <option value="15">15</option>
      <option value="16">16</option>
      <option value="17">17</option>
      <option value="18">18</option>
      <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
      <option value="22">22</option>
      <option value="23">23</option>
      <option value="24">24</option>
      <option value="25">25</option>
      <option value="26">26</option>
      <option value="27">27</option>
      <option value="28">28</option>
      <option value="29">29</option>
      <option value="30">30</option>
      <option value="31">31</option>
    </select>
        <input name="unitprice" value="0">
        <input name="linetotal" value="0">
        <input name="tax" value="0" />
         <a href="#" class="add">+</a>
    </div>
    <div class="line">
        Subtotal: <input id="subtotal">
        Tax: <input id="totaltax" />
    </div>   
</form>

i have done tax calculation coding separately. how can i integrate with above one. i mentioned above i done multiplication and tax calculation coding separately. how can i integrate these two coding?

<script>
var $k = jQuery.noConflict();

$k.fn.sum = function() {
    var sum = 0;
    $k(this).each(function() {
        sum += parseFloat($k(this).val());
    });

    return sum;
}

$k(function() {
    $k('input[name^=rate], input[name^=tax]').live("keyup", function() {
        var rate = parseFloat($k(this).parent().find('input[name^=rate]').val());
        var tax = parseFloat($k(this).parent().find('input[name^=tax]').val());
        $k(this).siblings('input[name^=nettotal]').val(rate/100 * tax);

        var sum = $k('input[name^=nettotal]').sum();

        $k('#finalnettotal').val(sum);
     });

    $k('.add').live('click', function() {
        $k(this).closest('.item').after($k('#template').html());
        $k('form .item').each( function(i) {
           $k(this).find('input[name^=num]').val(i+1);
        });
    });
});
</script>

i tried and finally i did it... if anyone wants javascript dynamic row with calculation na use this code.. it works perfectly...:-)

var $k = jQuery.noConflict();

$k.fn.sum = function() {
    var sum = 0;
    $k(this).each(function() {
        sum += parseFloat($k(this).val());
    });

    return sum;
}

$k.fn.calc = function() {
    var calc = 0;
    $k(this).each(function() {
        calc += parseFloat($k(this).val());
    });

    return calc;
}

$k.fn.final = function() {
    var final = 0;
    $k(this).each(function() {
        final += parseFloat($k(this).val());
    });

    return final;
}

$k(function() {
    $k('select[name^=noofnights], input[name^=unitprice], input[name^=tax]').live("keyup", function() {
        var noofnights = parseFloat($k(this).parent().find('select[name^=noofnights]').val());
        var unitprice = parseFloat($k(this).parent().find('input[name^=unitprice]').val());
        var tax = parseFloat($k(this).parent().find('input[name^=tax]').val());
        $k(this).siblings('input[name^=linetotal]').val(noofnights * unitprice);
        var linetotal = parseFloat($k(this).parent().find('input[name^=linetotal]').val());
        $k(this).siblings('input[name^=nettotal]').val(linetotal/100 * tax);
        var nettotal = parseFloat($k(this).parent().find('input[name^=nettotal]').val());

        var sum = $k('input[name^=linetotal]').sum();
        var calc = $k('input[name^=nettotal]').calc();
        var final = $k('input[name^=linetotal], input[name^=nettotal]').final();

         $k('.subtotal').val(sum);
         $k('#finalsubtotal').val(calc);
         $k('#finaltotal').val(final);
     });

    $k('.add').live('click', function() {
        $k(this).closest('.item').after($k('#template').html());
        $k('form .item').each( function(i) {
           $k(this).find('input[name^=num]').val(i+1);
        });
    });
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.