View Full Version : printing check box values using substring() HELP!!!

03-16-2005, 10:25 PM
Hi all,

I'm passing checkbox values between pages and have them collected (with submitted data) in hidden fields at the top of my form on page 2.

<input name="wcsave_b00_janfeb" type="hidden" value="January 3-February 25">
<input name="wcsave_b01_febapr" type="hidden">
<input name="wcsave_b02_mayjun" type="hidden" value="May 2-June 24">

When I try to write the values farther down in the page, they don't appear.

I'm trying to use the indexOf with the substring function that says loop thorugh every form field that starting with the name wcsave_b that has a value associated with it above, print out below. Here's the javascript I have:

var checkString = "wcsave_b"

checkString.substring(0, checkString.indexOf(" "))

for (var i=1; i < document.forms[0].elements[i].length; i++)
if(document.forms[0].elements.name[i] == "wcsave_b")
boxes = document.forms[0].elements[i].value.

How come the values don't print below?


03-16-2005, 11:27 PM
Well, your entire script appears to be out of whack... try taking it one step at a time (hint: the alert() method is handy).

03-17-2005, 04:27 PM
Here is the code watered down. It just checks to see if the checkbox with the value of wcsave_b0_janfeb is had a value passed and if it did, print it out

for (var i=1; i < document.forms[0].elements.length; i++)
{ if(document.forms[0].elements[i].name == "wcsave_b00_janfeb")
{ document.write(document.forms[0].elements[i].value)

this works great but I am unclear on what I need to do with the substring function to see that fields with (0,8) == wcsave_b0 then print out the value.

03-17-2005, 05:06 PM
I have no idea of what you're asking.

03-17-2005, 05:25 PM
I'm looping thru all of my form fields.

for (var i=0; i < document.forms[0].elements.length; i++)

The names I'm interested in are: wcsave_b01_janfeb, wcsave_b02_aprjun, wcsave_b03_sept_oct and so on. Since all of the names have wcsave_b in common, I want to say if any of those form field names if(document.forms[0].elements[i].name contain the string "wcsave_b"

then print out the value associated with the fields containing the string "wcsave_b".

document.write(document.forms[0].elements[i].value + "<br>")

03-17-2005, 05:32 PM
In that case, a regular expression would be simpler:

var rx = /^wcsave_b/;
// loop...

// do it...

03-18-2005, 03:38 PM
beautiful. Thank you!!!!