Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the textbox value when dropdown box value changed in javascript

I am having the table which contains the table like

items  price  quantity  total

apple    100     2         200

orange  200    2           600

grand total=600.

item fields are dropdown when drop down changes the price will be changed and total value and grandtotal also changed. My problem is when selecting apple and orange again go to apple change the item my grand total is not changing.

My Javascript code:

function totalprice(element, price) {
    var elementid = element.id;
    var expr = elementid.substring(elementid.indexOf(":") + 1, elementid.length);
    var quantity = document.getElementById("quantity:" + expr).value;
    var price = document.getElementById("price:" + expr).value;
    if (quantity > 0) {
        document.getElementById("total:" + expr).value = (parseInt(quantity)) * (parseInt(price));
        var grandtotal = document.getElementById("total:" + expr).value;
        var gtot = 0;
        var amount = 0;
        for (var i = 0; i <= expr; i++) {
            //document.getElementById("total").value="";
            gtot = document.getElementById("total:" + expr).value;
            amount = parseInt(gtot) + parseInt(amount);
        }
        document.getElementById("total").value = amount;
    }
    return true;
}

I know the mistake is in for loop only it is simple one but i dont know how to solve.

like image 753
Aravin Avatar asked Mar 08 '26 02:03

Aravin


1 Answers

I got the solution for this using table rows length and use that length to my for loop now my code is like

function totalprice(element,price)

{

var elementid=element.id;
var expr = elementid.substring(elementid.indexOf(":") + 1, elementid.length);
var quantity = document.getElementById("quantity:"+expr).value;

        var price = document.getElementById("price:" + expr).value; 
        if(quantity >0)
        {
        document.getElementById("total:"+ expr ).value= (parseInt(quantity))*(parseInt(price));           
        //var grandtotal =document.getElementById("total:"+expr).value;
        //var grandtotal = document.getElementsByClassName("total"+expr);

        var rowcount = document.getElementById('table').rows.length;

        var grandtotal = 0;
        var finalamount = 0;
            for(var i=1; i<rowcount; i++)
            {
                grandtotal=document.getElementById("total:"+i).value;
                finalamount = parseInt(grandtotal) + parseInt(finalamount);

            }

            document.getElementById("total").value=finalamount;
        }
      return true;

}

like image 184
Aravin Avatar answered Mar 10 '26 15:03

Aravin