Need Help in adding values of Multiple Fields containing same name using javascript
Hey guys I am new in Javascript as well as in this site. I have a small problem hope you guys can help me out
I will give a small example of the problem
HTML Code:
<html><body><form name="form"><input type="button" onclick="addInput()" name="add" value="Add more rows" /></form><div id="row"><input name="qty" type='text' value=''" /> <br /> // This is the quantity field
<input name="price" type='text' value=''" /> // this is price field
<input name="total" type='text' value=''" /> // This field should multiply the values of qty and price
</div>
<div id="row">
<input name="qty" type='text' value=''" /><br /> // This is the quantity field
<input name="price" type='text' value=''" /> // this is price field
<input name="total" type='text' value=''" /> // This field should multiply the values of qty and price
</div><input name="grandTotal" type='text' value='' /></body></html>
Now in the above code there are two set of rows containing input fields
I have used the same name in the input field cuz in future if a user clicks on a Add more row he can have as many as he wants so how do I apply addition in all the generated fields dynamically using javascript and display in in the an answer input. this is just an example I will have more fields in the same form but just to be on point I have written this example.
Now what I want to know here is how to multiply the qty and price field and display the answer in the total field. And then add the total fields and display the answer in grandTotal field.
Hope I didn't confuse and you guys and would provide me an easy solution.
thanx I appreciate the effort shown from both of you guys. As a beginner in this language it really feels easy to get ahead when there are guys like you to help
hey vwphillips can you also provide a script to delete row in front of each row and last but not least one more field of discount. I hope you dont mind. I find your scripts are really easy for a beginner to learn.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function addInput(frm){
var frm=document.forms[0],div=frm.getElementsByTagName('DIV')[0].cloneNode(true);
frm.insertBefore(div,frm.getElementsByTagName('A')[0]);
}
function Totals(){
var frm=document.forms[0],flds=[frm['qty'],frm['price'],frm['total']],total=0;
for (var z0=0;z0<flds[0].length;z0++){
if (isFinite(flds[0][z0].value*1)&&isFinite(flds[1][z0].value*1)){
flds[2][z0].value=flds[0][z0].value*flds[1][z0].value;
total+=flds[2][z0].value*1;
}
}
frm['grandTotal'].value=total;
}
/*]]>*/
</script></head>
<body>
<html><body>
<form name="form">
<input type='text' id='userInput' value='Enter Percentage Change' />
<div id="row">
<input type="text" name="Default_Price[]" value="9.000000" >
</div>
<div id="row">
<input type="text" name="Default_Price[]" value="94.000000" >
</div>
<a></a>
<input type="button" name="" value="Update" onmouseup="Totals();" />
</form>
</body>
</html>
I am a novice at best with Javascript and any help would be appreciated. I am having a hard time trying to figure out what I need to do here. I ran across this thread and I modified the html form to display what I am trying to do, however everything else is just a copy past from a solution provided by vwphillips. I am dynamically generating a form using php and I want to be able to adjust all prices in the form by entering a Percentage in the first field then applying the percent of change to all prices on the form positive or negative.
Bookmarks