Forum Moderators: open

Message Too Old, No Replies

Getting checkbox value

         

ntbgl

2:12 am on Feb 19, 2009 (gmt 0)

10+ Year Member



I'm trying to get the values of some checkboxes in my form using javascript, but I'm having some problems getting my javascript to respond, so I've broken it into baby steps, and now all I'm trying to do is to get the lenght of checkbox so I could create a for statment to cycle through, but I can't even do that.

alert(document.reply.cb.length); works if my checkboxes look like this:

<input type="checkbox" name="cb" value="1" />
<input type="checkbox" name="cb" value="2" />

But they don't, they look like this:

<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />

I've even tried changing my alert test to this:

alert(document.reply.cb[].length);

I need the cb to have the [] suffix so I can pass multiple values through to the receiving page. Isn't that what checkboxes are for?

daveVk

3:20 am on Feb 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



alert(document.reply.["cb[]"].length);
will probably work to the extent that
alert(document.reply.cb.length); works

However not sure wisdom of adding [] suffix, what was named cb is now named cb[] ( invalid syntax for javascipt name ), perhaps it serves some server side utility ?

Rambo Tribble

10:46 pm on Feb 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More to the point, "[]" are not valid characters in an HTML attribute. I've seen them used in PHP scripts to create arrays of returned values, but they shouldn't be sent to the browser that way.

Checkboxes are intended to be independent elements, not influenced by other checkboxes in a page. As such, they should have independent names. Normally, it is only a group of radio buttons which share a name.