onchange for dynamically added rows

Hi ,

I am new to javascript and have coded an invoice page through help from some available

content on web.

here is my requirement

My form contains inputs "item ,rate quantity,price" in a tabular form where one can

dynamically add or delete rows.And the requirement is that

when Quantity is entered the Price should change to Price=Quantity*Rate on tab out

So I have used a Javascript onchange() and this works fine for the first displayed row.

but for dynamically added rows this does not happen, I am not able change the price

value for dynamically added rows.

Code is as below

<table align="center" width = "75%">
<td align = "center">
<!--- very imporant to give the table an id --->
<!--- otherwise it won't know where to edit --->
<table border="1" id="mySampleTable">
<th>Item No</th>
<th>Item Name</th>
<th>Unit Cost</th>
<th> <font color="RED">Delete</font></th>
<td><input type="text" name="itemname[]"  size="40" "maxsize="100"/></td>
<td><input type="text" name="description[]" size="20" " maxsize="100" /></td>
<td><input type="text" id = "unitcost[]" name="unitcost[]" size="10" maxsize="100" 

<td><input type="text" id = "quantity[]" name="quantity[]" onchange = "onc();" size="4" 

maxsize="100" /></td>
<td><input type="text" id = "price[]" name="price[]" size="10" maxsize="100" /></td>
<table id="totaltbl" align="right">
<th>Vat %</th>
<td><input type="text" name="total" size="3" maxsize="3" /></td>
<td><input type="text" id = "total"  name="total" size="20" maxsize="100" /></td>

<form action="save_entry.php" name="eval_edit" method="post" format="html">
<input type="button" value="Add Row" onclick="addRow();" />	
<input type="button" value="Calculate Total Amount" onclick="Totalcal();" />	
<input type="button" value="Print" />


my java script for dynamically adding rows works as below for each cell

for newly added row the columnQuantit will be created like this

Add row()

var cellRight4 = row.insertCell(4);
var e4 = document.createElement('input');
e4.type = 'text';
e4.name = 'quantity[]';
e4.id = 'quantity[]';
e4.size = 4;
e4.maxsize = 100;

calculate price:

function onc() {

var a= document.getElementById('unitcost[]');
var b= document.getElementById('quantity[]');
var c = Math.ceil(a.value*b.value);
document.getElementById('price[]').value =c;
October 25,
Not the right place to post basic html questions, as this is a forum for Activewidgets product, anyway I`d try (just once) A fast review to your code thoghts:

<td><input type="text" id = "quantity[]" name="quantity[]"   onchange = "onc(this.value);" size="4" maxsize="100" /></td>

note the "this.value"
e4.id = 'quantity[]';
e4.onchange = "onc(this.value);"  //New Line
e4.size = 4;

October 25,

This topic is archived.

See also:

Back to support forum