Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic form field names

Status
Not open for further replies.

taz6053

Programmer
Feb 12, 2007
9
US
I have a form with textfields that are dynamically created and named from a recordset. There are 2 textfields created by each record Ex. sAccountId1, sAmount1 and are incremented based on the number of records. The record count is stored in another textfield. I need to use javascript to add up all of the sAmount1 thru sAmount...20 when one of those textfields change. I have tried using a for loop but am not real sure how to do this. I am really new to javascript. I have mostly been programming in vbscript in the past.

Any help would be appreciated.
 
The following will return the total of all text fields where the first seven characters of the name attribute are "sAmount".

Code:
function getTotal(){
 var myForm = document.getElementById("myFormID");
 var allAmounts = myForm.getElementsByTagName("input");
 var total = 0;
 for(var i = 0; i < allAmounts.length; i++){
  var currField = allAmounts.item(i);
  if(currField.type == "text" && currField.name.slice(0,7) == "sAmount"){
   total = total + parseFloat(currField.value);
  }
 }
 return total;
}

- Provided of course that all those values are numeric. If a user enters text in there it will spit our a NaN (Not a Number). Adding some error checking like:

Code:
  var currFieldVal = parseFloat(currField.value);
  if(isNaN(currFieldVal)){
   alert("Unable to convert value to number in field " + currFieldVal.name);
  }
  else{
   total = total + currFieldVal ;
  }

Would help if you don't already have some functionality in there enforcing numeric only values.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Thanks for your help but I am getting an error the error 'null' is null or not an object. By prcess of elimination the line getting this error is var allAmounts = myForm.getElementsByTagName("input");
 
var myForm = document.getElementById("myFormID");

I copied the code exactly from what you wrote above.
 
Right... and your form's ID just so happens to be "myFormID"?

Try replacing that with the actual ID of your form (sorry, but telepathy is not my strong suit).

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Thanks again. I had tried changing that but wasn't sure what to change it to. Below is what I ended up with and it is working.

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function s(num) {
amount = num * 1; // amount is the num or NaN

if (isNaN(amount)) { // if the entire is not a number

alert(
"' " + num + " ' is not a valid entry for that field."
);

return 0;
}
else
return amount; // if it is OK, send sum back
}

function r2(n) {
//round all values to 2 decimal places
ans = (Math.round(n * 100))/100 + ""
dot = ans.indexOf(".",0)
if (dot == -1) {ans = ans + ".00"}
else if (dot == ans.length - 2) {ans = ans + "0"}
return ans
}



function locked(a) {
window.event.returnValue=false
alert("This is a calculated field.")
}

function getTotal(form){

var myForm = document.getElementById("InvAdd");
var allAmounts = myForm.getElementsByTagName("input");
var total = 0;
for(var i = 0; i < allAmounts.length; i++){
var currField = allAmounts.item(i);
if(currField.type == "text" && currField.name.slice(0,7) == "sAmount"){
var sAmt=s(currField.value);
currField.value=r2(sAmt)
total=total + sAmt;
}
}

form.sum.value=r2(total)
}

// End -->
 
I think I'm half psychic and half psycho, and that's why I thought the line before had an error in it. :)#

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top