0

I am trying to calculate amount without VAT from a pre-calculated amount with VAT . The HTML form looks like :

<table>
        <tr>
            <th>Sl No</th>
            <th>Description of goods</th>
            <th>Price</th>
            <th>VAT%</th>
            <th>Price with VAT</th>
            <th>Quantity</th>
            <th>Amount</th>
        </tr>   

    {foreach name = feach item = k from = $ip key = ind}
        <tr>
            <td>{$ind+1}</td>
            <td>{$k->brand},{$k->model}</td>
            <td id="prd_prc">&nbsp;</td>
            <td id="vat{$ind}">
                <select name="vatpc" id="vatpc" onchange="calculate('{$ind}')">
                    <option value="5">5</option>
                    <option value="13.5">13.5</option>
                </select>
            </td>
            <td id="total_cost{$ind}">{$k->price}</td>
            <td>{$k->quantity}</td>
            <td>{$k->total_cost}</td>
        </tr>
    {/foreach}
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>Total Cost </td>
            <td>{$final_amount}</td>
        </tr>
    </table>

And the JavaScript function is :

function calculate(idno)
{
    //alert(idno);
    var vat_pc = document.getElementById("vatpc").value;


    var total_cost = document.getElementById("total_cost"+idno).value;

    //total_cost = Number(total_cost);
    alert(total_cost);

    //vat_pc = parseFloat(vat_pc);
    // = v + 2.10 + 11.25;
    //alert(v);
    // document.getElementById("total").innerHTML = "Total - amount : "+v+" USD";
}

But the alert shows undefined. I tried adding innerHTML,but results same. Please help. Thanks in advance

1
  • try to find out the value for idno , it is not coming mostly ... Commented Feb 2, 2012 at 6:37

2 Answers 2

2

total_costX is a <td> element. Use the textContent property.

var total_cost = document.getElementById("total_cost"+idno).textContent;
Sign up to request clarification or add additional context in comments.

3 Comments

Ya...I put the lines exactly according to you . Still its a NaN !
remove the parseFloat and see what is alerted.
Ya..the value within the id <td id="total_cost{$ind}">
0

There in nothing like value of dive. But you can get text or html inside dive. if you are using jquery, it should be quite simple:

$(document.getElementById("vatpc")).text();
//OR
$(document.getElementById("vatpc")).html();

Or jQuery's way

$("#vatpc").text(); //OR $("#vatpc").html();

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.