Jump to content

[SOLVED] get the sum of many text box values


aliasneo86

Recommended Posts

I have a editable grid form in the transactions page. in this there is a column named amount and the no of text box values under that is unknown how can I get a java script to to the sum of all the text boxs in that column?

 

the pic of the page is attached

 

thanks in advance :)

 

[attachment deleted by admin]

Link to comment
Share on other sites

I suggest giving the textboxes, when they are generated, a class name that you'll only use on them. For example, addition_textbox. Then you can apply the following to it:

 

<script language="javascript">
function calculate_total(className, div){
	var elements = document.getElementsByClassName(className);
	var total = 0;

	for(var i = 0; i < elements.length; ++i){
		total += parseInt(elements[i].value);
	}

	document.getElementById(div).innerHTML = "Total: " + total;
}
</script>
<input type="text" name="box1" id="box1" class="addition_textbox" value="4" /><br />
<input type="text" name="box2" id="box2" class="addition_textbox" value="1" /><br />
<input type="text" name="box3" id="box3" class="addition_textbox" value="14" /><br />
<input type="text" name="box4" id="box4" class="addition_textbox" value="214" /><br />
<input type="text" name="box5" id="box5" class="addition_textbox" value="2" /><br />
<input type="text" name="box6" id="box6" class="addition_textbox" value="8" /><br />
<input type="text" name="box7" id="box7" class="addition_textbox" value="3" /><br />
<input type="text" name="box8" id="box8" class="addition_textbox" value="16" /><br /><br />
<input type="submit" name="calculate" id="calculate" value="Calculate Total" onclick="calculate_total('addition_textbox', 'total_text')" /><br />

<div id="total_text"></div>

Link to comment
Share on other sites

can you see what wrong in this that i used ur own code

 

<!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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

 

<script language="javascript">

  function calculate_total(className, div){

      var elements = document.getElementsByClassName(addition_textbox);

      var total = 0;

     

      for(var i = 0; i < elements.length; ++i){

        total += parseInt(elements.value);

      }

     

      document.getElementById(div).innerHTML = "Total: " + total;

  }

</script>

</head>

<body>

<form name="form1" method="get">

<input type="text" name="box1" id="box1" class="addition_textbox" value="4" /><br />

<input type="text" name="box2" id="box2" class="addition_textbox" value="1" /><br />

<input type="text" name="box3" id="box3" class="addition_textbox" value="14" /><br />

<input type="text" name="box4" id="box4" class="addition_textbox" value="214" /><br />

<input type="text" name="box5" id="box5" class="addition_textbox" value="2" /><br />

<input type="text" name="box6" id="box6" class="addition_textbox" value="8" /><br />

<input type="text" name="box7" id="box7" class="addition_textbox" value="3" /><br />

<input type="text" name="box8" id="box8" class="addition_textbox" value="16" /><br /><br />

<input type="submit" name="calculate" id="calculate" value="Calculate Total" onclick="calculate_total('addition_textbox', 'total_text')" /><br />

 

<div id="total_text"></div>

</form>

 

 

</html>

Link to comment
Share on other sites

the error msg pic

 

code

====================

function calculate_total(className, div){

      var elements = document.getElementsByClassName(className);

      var total = 0;

     

      for(var i = 0; i < elements.length; ++i){

        total += parseInt(elements.value);

      }

      alert ("test");

      document.getElementById(div).innerHTML = "Total: " + total;

  }

 

text box

 

<input style="WIDTH: 88px; HEIGHT: 22px; TEXT-ALIGN: right" maxlength="20" size="12" value="{amount}" name="{amount_Name}" id="{amount_Name} class="addition_textbox"">

 

 

 

======================

 

[attachment deleted by admin]

Link to comment
Share on other sites

Okay we'll avoid doing it that way since it's not available for all browsers.

 

Try this instead:

 

<script language="javascript">
function calculate_total(className, div){
	var elements = document.getElementsByTagName("input");
	var total = 0;

	for(var i = 0; i < elements.length; ++i){
		if(elements[i].className == className){
			total += parseInt(elements[i].value);
		}
	}

	document.getElementById(div).innerHTML = "Total: " + total;
}
</script>
<input type="text" name="box1" id="box1" class="addition_textbox" value="4" /><br />
<input type="text" name="box2" id="box2" class="addition_textbox" value="1" /><br />
<input type="text" name="box3" id="box3" class="addition_textbox" value="14" /><br />
<input type="text" name="box4" id="box4" class="addition_textbox" value="214" /><br />
<input type="text" name="box5" id="box5" class="addition_textbox" value="2" /><br />
<input type="text" name="box6" id="box6" class="addition_textbox" value="8" /><br />
<input type="text" name="box7" id="box7" class="addition_textbox" value="3" /><br />
<input type="text" name="box8" id="box8" class="addition_textbox" value="16" /><br /><br />
<input type="submit" name="calculate" id="calculate" value="Calculate Total" onclick="calculate_total('addition_textbox', 'total_text')" /><br />

<div id="total_text"></div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.