homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
dynamic input fields
retrieving the values
mattglet




msg:1476137
 6:07 pm on Nov 18, 2003 (gmt 0)

i have a form that consists of dynamic input field names.

i.e.

weight1
weight2
weight3
etc.

i need to figure out how to obtain the total value of all the fields, before submitting the form.

my feeble attempts were variations of this:

var n = 0;
var i = 0;
var x = 0;
var form = "form100";
var prefix = "weight";

while (form[prefix + i++]) {
x = form[prefix + i++].value;
n = n + x;
}

then check the value of n. can anyone help?

-Matt

 

mikegram




msg:1476138
 11:18 pm on Nov 18, 2003 (gmt 0)

weight1
weight2
weight3
...

These are foo-generated text fields? And the number varies from page to page?

What was the result of the script you have there? Is that script contained in a function?

mattglet




msg:1476139
 11:37 pm on Nov 18, 2003 (gmt 0)

sorry, i figured out the problem, and here's the result:

// this function checks the values of the dynamically generated fields.
// the prefix is the field name i.e. <input type = "text" name = "weight<%=count%>">
// the Check100 function adds up all entered values in the fields, and makes sure the total value is 100. if it's not 100, throw error.

function Check100() {
var n = 0;
var i = 0;
var x = 0;
var prefix = "weight";

while (++i <= <%=n%>) { // this n variable is an ASP variable pulled from a form
x = (document.checkform[prefix + i].value) - 0; // subtracting zero from a string, converts it to type integer
n = eval(n + x); // keep track of cumulative total
}

if (n!= 100) {
alert('Your values must equal 100');
return false;
}
else {
return true;
}
}

-Matt

mikegram




msg:1476140
 2:39 am on Nov 19, 2003 (gmt 0)

x = (document.checkform[prefix + i].value) - 0; // subtracting zero from a string, converts it to type integer
n = eval(n + x); // keep track of cumulative total

The toInt hack is a neat trick.

Why eval()? Just added insurance in case that doesn't work and x is still a string?

mattglet




msg:1476141
 3:23 am on Nov 19, 2003 (gmt 0)

i couldn't find quickly enough how to convert a string to integer in javascript, but i did find that little trick first. i was having a problem where my values were concatenating, instead of adding, and the subtract zero thing worked.

yeah, i figured the eval() doesn't hurt anything, so i kept in there. like you said, it's just added insurance.

-Matt

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved