/    Sign up×
Community /Pin to ProfileBookmark

Calculating Values

Hello friends.

I got a problem. I have a form where I display quantity, value, and total. These fields are generated dynamically in php and when you put a value in the field value, automatically the field total is calculated depending of the quantity.

Here is the code.

[CODE]
function calcularTotalParcial(index){
var elementos = document.getElementsByName(“total”);
var cantidad = document.getElementsByName(“cantidad”)[index];
var valor = document.getElementsByName(“valor”)[index];
var total = document.getElementsByName(“total”)[index];
for(var i=0; i<elementos.length; i++){
if((cantidad.value != “”) && (valor.value != “”) && (elementos.value != “”)){
total.value = cantidad.value*valor.value;
calcularTotal();
}
else{
if(total.value != “”){
total.value = “”;
calcularTotal();
}
}
}
}

function calcularTotal(){
var totales = document.getElementsByName(“total”);
var suma = 0;
for(var i=0; i<totales.length; i++){
if(totales[i].value != “”){
suma += totales[i].value*1;
}
}
document.getElementById(“total_venta”).value = suma;
}
[/CODE]

and the form is like this.

[CODE]
<input name=”cantidad” type=”text” value=”<? echo $row1[‘a198Cantidad’];?>” size=”3″ onKeyUp=”calcularTotalParcial(1)”></td>
<td><input type=”text” name=”valor” onKeyUp=”calcularTotalParcial(1)”></td>
<td colspan=”2″><input name=”total” type=”text” readonly=”true”></td>
[/CODE]

As you can see I’m passing the position 1 to the function and it works fine. But it only calculates the position 1. I need that when I put a value in eachone of the input name=value it calcualtes for eachone not for only one position. I think that the question is how can I pass the n input texts to the function.

Thanks!!!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsJun 30.2005 — [CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--

var Ary=new Array();

function Cal(obj){
if (isNaN(obj.value)){ return; }
if (!obj.cal){
obj.cal=true;
Ary[Ary.length]=obj;
}
total=0;
for (i=0;i<Ary.length;i++){
total+=parseInt(Ary[i].value);
}
document.getElementById('Total').value=total;
}
//-->
</script>


</head>

<body>

<input name="" size="10" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>

Total <input id="Total" size="10">

</body>

</html>

[/CODE]
Copy linkTweet thisAlerts:
@freemojorisinauthorJun 30.2005 — it doesn't work because for the first input text it is not calculated and for the second input text it does. I dont know what to do and I'm trying to get this as soon as possible here in my work.

Thanks
Copy linkTweet thisAlerts:
@vwphillipsJun 30.2005 — it doesn't work because for the first input text it is not calculated [/QUOTE]

The code works for any 4 inputs that has a keyup event


TESTED!

however if key up events are not guaranteed add the obj to the array

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--

var Ary=new Array();

function Init(obj){
ips=document.getElementById(obj).getElementsByTagName('INPUT');
for (i=0;i<ips.length;i++){
Ary[Ary.length]=ips[i];
ips[i].cal=true;
}
}

function Cal(obj){
if (isNaN(obj.value)){ return; }
if (!obj.cal){
obj.cal=true;
Ary[Ary.length]=obj;
}
total=0;
for (i=0;i<Ary.length;i++){
if (!isNaN(Ary[i].value)&&Ary[i].value!=''){
total+=parseInt(Ary[i].value);
}
}
document.getElementById('Total').value=total;
}

//-->
</script>


</head>

<body onload="Init('fred');" >
<span id="fred" >
<input name="" size="10" value="100" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>
<input name="" size="10" onkeyup="Cal(this);"> <br>
</span>
Total <input id="Total" size="10">

</body>

</html>

[/CODE]
×

Success!

Help @freemojorisin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...