How do I get the SUM of 'a' textfields in javascript before I submit the form?

Hi All!
I already have the solution (provided by und3ath) to this question but this query is related to the solution. Please do refer to the solution (ID:23665873). To get the no. of rows of html elements I had iterated '10' times. What I need to do now is change that value from '10' to a variable say 'a'. This variable gets its value from a textfield (name="a"). The iteration works fine but the Javascript function getsum() does not work. why is that? please can anyone help me?
<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum">
          </table>
          <script type="text/javascript">
function getsum(){var sum=0;
for(i=1;i<=10;i++)
    sum+=Number(document.getElementById('total_wt'+i).value);
document.getElementById('sum').value=sum;}
 
setInterval("getsum()",1);
</script>

Open in new window

786aslamkhanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darkappleCommented:
don't refrence text elements in form by document.getElementById...

try

document.formName.elementName.value it should work fine i guess.

0
hieloCommented:
try:
<form name="myForm">
<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum" value="">
          </table>
</form>
          <script type="text/javascript">
function getsum(){
	var f=document.forms.myForm;
	var sum=0;
	for(var i=0, limit=f.elements.length; i < limit; i++)
	{
		if( f.elements[i].name.indexOf("total_wt")==0 )
    			sum+=Number(f.elements[i].value);
	}
	document.getElementById('sum').value=sum;
}
 
setInterval("getsum()",1);
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
quincydudeCommented:

<table>
<?php
        for ($i = 1;$i <= 10;$i++) {
?>
    <tr>
 
<td>
        <label>
        <input name="total_pcs<?php echo $i; ?>" type="text" id="total_pcs<?php echo $i; ?>" value="<?php  echo $_POST['total_pcs' . $i]; ?>"/> 
        </label>
      </span></td>
      <td>
        <label>
        <input name="total_wt<?php echo $i; ?>" type="text" id="total_wt<?php echo $i; ?>" value="<?php  echo $_POST['total_wt' . $i]; ?>"/>
        </label>
      </td></tr>
<?php
        }
?>
 
          <input type="text" id="sum">
          </table>
          <script type="text/javascript">
function getsum(){var sum=0;
for(i=1;i<=10;i++)
{
	if(document.getElementById('total_wt'+i) != null)
		sum+=Number(document.getElementById('total_wt'+i).value);
}
document.getElementById('sum').value=sum;}
 
setInterval("getsum()",1);
</script>

Open in new window

0
786aslamkhanAuthor Commented:
Thanks a ton hielo! The soultion is absolutely perfect. Thanks for your time, attention and help. You are a true GENIUS!!! I have learnt this from you!
0
786aslamkhanAuthor Commented:
Thanks quincydude for your solution- I had already tried and accepted  hielo's solution (and Awarded the points) which is perfect. I have not yet tried yours but I will later just for learning. However please do accept my thanks and gratitude for your precious time, attention and offered solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.