Adding 2 text fields values in javascript using array

hi ,

I have 2 text fields , i want to add the value of the two text fields and show the sum on the third field on onclick. But my problem is field name and id is an array.

<form name=“mpr_audit” action=“audit_action.php” method=“post”>
<input type=“no1” id=“no1” />
<input type=“no2” id=“no2” />
<input type=“no3” id=“no3” />
</form>

The form name is useless and misleading. Make it an id attribute instead, and your scripting will be able to more easily work with the elements of the form.
Typically this is done by using the name attribute to name the fields of the form that you intend to submit.

<form id="mpr_audit" action="audit_action.php" method="post">
    <input type="no1[]" name="no1[]" />
    <input type="no2[]" name="no2[]" />
    <input type="no3[]" name="no3[]" />
</form>

The benefit of doing things this way, is that the only possible conflicting part of the form is now the single id attribute on the form tag, which should be a different identifier on a different form anyway to represent what it is that the form does.


var form = document.getElementById('mpr_audit');

Now that you have a reference to the form, you can easily access the named elements within it by using the elements collection:


var form = document.getElementById('mpr_audit'),
    total = 0;
total += Number(form.elements['no1[]'].value) || 0;
total += Number(form.elements['no2[]'].value) || 0;
form.elements['no3[]'].value = total;

The “|| 0” part means that if we don’t get a valid number from the field, we will default to using 0 instead.

<form id=“progress_report_demand” action=“audit_section_2_action.php” method=“post” enctype=“multipart/form-data”>

<table width=“100%” border=“0” cellspacing=“3” cellpadding=“2”>
<tr>
<td rowspan=“2”><strong>Type Of The Societies , Directorate Wise</strong></td>
<td rowspan=“2”><strong>Amount Of Audit Fees Outstanding At The Beginning Of The Year</strong></td>
<td rowspan=“2”><strong>Amount Of Audit Fees Levied Up To The Beginning Of The Month</strong></td>
<td rowspan=“2”><strong>Amount Of Audit Fees Levied During the d_month</strong></td>
<td rowspan=“2”><strong>Total Amount Of Audit Fees Levied From The Beginning Of The Year Till End Of The Month</strong></td>
<td rowspan=“2”><strong>Total Demand For Collection</strong></td>

</tr>
<tr>
<td><strong>Out Of Arrear</strong></td>
<td><strong>Out Of Current</strong></td>
<td><strong>Out Of Arrear</strong></td>
<td><strong>Out Of Curren</strong></td>
<td><strong>AS On</strong></td>
<td><strong>As On</strong></td>
</tr>
<?php
$result=$db->query(“SELECT * FROM society_type”);
while ($line1 = $db->fetchNextObject($result)) {
$society_id=$line1->society_type_id;
?>
<tr>
<td>
<strong><?php echo $line1->society_name; ?></strong></td>
<td><input type=“text” size=“11” name=“outstanding_fees” id=“outstanding_fees” /></td>
<td><input type=“text” size=“11” name=“levied_fees” id=“levied_fees”/></td>
<td><input type=“text” size=“11” name=“levied_fees_dumonth” id=“levied_fees_dumonth” /></td>
<td><input type=“text” size=“11” name=“total_amt_levied_fees” id=“total_amt_levied_fees” onclick=“javascript:add1();”/></td>

</tr>
<?php }?>
<tr>
<td height=“26”><input type=“submit” name=“submit” value=“Delete Selected” onclick=“return confirm(‘Are you sure you want to delete?’)”/>
</td>
<td colspan=“”>
<?php
if($progress_report_demand_id==‘’){ ?>
<input type=“submit” id=“submit_form” name=“submit_form” value=“Submit” />
<?php }else{ ?>
<input type=“hidden” name=“progress_report_demand_id” value=“<?php echo $progress_report_demand_id; ?>” />
<input type=“submit” name=“submit_form” id=“submit_form” value=“Update”/>
<?php } ?>
</td>
</tr>
</table>
</form>

I wrote the code but it is not working due to while loop… so plz tell me how to do that

The easiest way would be to loop through the entire form, adding up form.elements[‘outstanding_fees[]’][i] and form.elements[‘levied_fees[]’][i] and other associated fields, assigning the result to form.elements[‘total_amt_levied_fees[]’][i]