how to get the values of checkboxlist in javascript?

M

Mark

Hi,

i need all the values of a checkboxlist (selected or not) in a javascript
string (separated by a ';').
The checkboxlist is created dynamically in code-behind. The different
options of the lisitems are passed to javascript via a hiddenfield.

I can realize this with radiobuttonlist, and in order to make me understood,
i show it first:

this is (part of) the source of the html page received by the server:
-----------------------------------------------------------------
<td><input id="vrg2_0" type="radio" name="vrg2" value="1" /><label
for="vrg2_0">0</label></td>
<td><input id="vrg2_1" type="radio" name="vrg2" value="2" /><label
for="vrg2_1">1</label></td>
<td><input id="vrg2_2" type="radio" name="vrg2" value="3" /><label
for="vrg2_2">2</label></td>

and this is (part of) the javascript code:
--------------------------------------
// i=2
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw=document.getElementById("vrg"+i+"_"+j).value

This works.


Now the checkboxlist:
this is (part of) the source of the html page received by the server: (i
can't see a 'value' property here!)
-----------------------------------------------------------------
<td><input id="vrg3_0" type="checkbox" name="vrg3$0" /><label
for="vrg3_0">a</label></td>
<td><input id="vrg3_1" type="checkbox" name="vrg3$1" /><label
for="vrg3_1">z</label></td>
<td><input id="vrg3_2" type="checkbox" name="vrg3$2" /><label
for="vrg3_2">e</label></td>

and this is (part of) the javascript code:
--------------------------------------
// i=3
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw=antw+";"+document.getElementById("vrg"+i+"_"+j).value
}
else
{ antw=antw+";0" }
}

This doesn't work.Suppose i check the second option, I get
"undefined;0;on;0".
The two 0 are ok, because they are the two unchecked options, but
'Undefined' and 'on'???

So, how can i get the values (selected or not) of the checkboxlist?
Thanks
Mark
 
B

bruce barker

the checkboxlist does not render the values as the checkboxes all have unique
names (unlike the radiolist, where the value is used to determine which one
is posting back). so you can't directly get a checkbox value from javascript.
you will need to pass the values from the server to javascript in a hidden
field.

-- bruce (sqlwork.com)
 
M

Mark

Thanks

bruce barker said:
the checkboxlist does not render the values as the checkboxes all have
unique
names (unlike the radiolist, where the value is used to determine which
one
is posting back). so you can't directly get a checkbox value from
javascript.
you will need to pass the values from the server to javascript in a hidden
field.

-- bruce (sqlwork.com)


Mark said:
Hi,

i need all the values of a checkboxlist (selected or not) in a javascript
string (separated by a ';').
The checkboxlist is created dynamically in code-behind. The different
options of the lisitems are passed to javascript via a hiddenfield.

I can realize this with radiobuttonlist, and in order to make me
understood,
i show it first:

this is (part of) the source of the html page received by the server:
-----------------------------------------------------------------
<td><input id="vrg2_0" type="radio" name="vrg2" value="1" /><label
for="vrg2_0">0</label></td>
<td><input id="vrg2_1" type="radio" name="vrg2" value="2" /><label
for="vrg2_1">1</label></td>
<td><input id="vrg2_2" type="radio" name="vrg2" value="3" /><label
for="vrg2_2">2</label></td>

and this is (part of) the javascript code:
--------------------------------------
// i=2
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw=document.getElementById("vrg"+i+"_"+j).value

This works.


Now the checkboxlist:
this is (part of) the source of the html page received by the server: (i
can't see a 'value' property here!)
-----------------------------------------------------------------
<td><input id="vrg3_0" type="checkbox" name="vrg3$0" /><label
for="vrg3_0">a</label></td>
<td><input id="vrg3_1" type="checkbox" name="vrg3$1" /><label
for="vrg3_1">z</label></td>
<td><input id="vrg3_2" type="checkbox" name="vrg3$2" /><label
for="vrg3_2">e</label></td>

and this is (part of) the javascript code:
--------------------------------------
// i=3
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw=antw+";"+document.getElementById("vrg"+i+"_"+j).value
}
else
{ antw=antw+";0" }
}

This doesn't work.Suppose i check the second option, I get
"undefined;0;on;0".
The two 0 are ok, because they are the two unchecked options, but
'Undefined' and 'on'???

So, how can i get the values (selected or not) of the checkboxlist?
Thanks
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top