Looping through form values

Results 1 to 2 of 2

Thread: Looping through form values

  1. #1
    Join Date
    Dec 1969

    Default Looping through form values

    I am only posting this here because I was told that the ASP forum was probably the wrong spot to post the question. The url to the question is below if anyone can help. Thanks<BR><BR>http://www.aspmessageboard.com/forum/asp.asp?M=297918&P=1&F=20<BR><BR>Harry C

  2. #2
    Join Date
    Dec 1969

    Default Martini gave you the answer...

    Dunno why you were told to post here, though...<BR><BR>This is client-side JavaScript and so is actually *more* appropriate there then here.<BR><BR>Anyway, a group of same-named input fields of *ANY* kind is always treated the same way by DOM: The name now references an array, and the fields are the elements of the array. <BR><BR>You see this behavior with same-named checkboxes, and it is no different for same named hidden fields.<BR><BR>In point of fact, you *could* (if you were foolish enough) name a checkbox, a radio button, a text area, and a button all the same...and they&#039;d become an array! The arrays do *NOT* have to be homogeneous! Obviously, the coding is a lot simpler if they are.<BR><BR>Anyway...<BR><BR>function GetCreditValue() { <BR>&nbsp; &nbsp; var sum = 0;<BR>&nbsp; &nbsp; var ctl = document.form1.ctlPCredits; // ctlPCredits is the group name, yes?<BR>&nbsp; &nbsp; for (var i=0; i &#060; ctl.length; i++) // .length is length of the array<BR>&nbsp; &nbsp; { <BR>&nbsp; &nbsp; &nbsp; &nbsp; sum += parseFloat( ctl[ i ].value );<BR>&nbsp; &nbsp; } <BR>&nbsp; &nbsp; return sum;<BR>}<BR><BR>Of course, you could use parseInt if you know/expect all the values to be integers. You could also use isNaN to check for non-numeric values. But since I assume you will be putting the values into the hidden fields and will therefor know what they are, I didn&#039;t see the need for it.<BR><BR>Note that you *do* need the parseFloat or parseInt. The .value property of *any* form field is *always* a text string!<BR><BR>And Martini gave you that answer, but you had the .value where you needed the .length so of course it didn&#039;t work!<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts