how to get the values of checkboxlist in javascript?

how to get the values of checkboxlist in javascript?

am 04.01.2008 23:47:34 von 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:
------------------------------------------------------------ -----




and this is (part of) the javascript code:
--------------------------------------
// i=2
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw[i]=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!)
------------------------------------------------------------ -----




and this is (part of) the javascript code:
--------------------------------------
// i=3
for (j=0;j<=2;j++)
{
if (document.getElementById("vrg"+i+"_"+j).checked)
{
antw[i]=antw[i]+";"+document.getElementById("vrg"+i+"_"+j).v alue
}
else
{ antw[i]=antw[i]+";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

RE: how to get the values of checkboxlist in javascript?

am 05.01.2008 00:15:00 von brucebarker

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" wrote:

> 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:
> ------------------------------------------------------------ -----
>
>
>
>
> and this is (part of) the javascript code:
> --------------------------------------
> // i=2
> for (j=0;j<=2;j++)
> {
> if (document.getElementById("vrg"+i+"_"+j).checked)
> {
> antw[i]=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!)
> ------------------------------------------------------------ -----
>
>
>
>
> and this is (part of) the javascript code:
> --------------------------------------
> // i=3
> for (j=0;j<=2;j++)
> {
> if (document.getElementById("vrg"+i+"_"+j).checked)
> {
> antw[i]=antw[i]+";"+document.getElementById("vrg"+i+"_"+j).v alue
> }
> else
> { antw[i]=antw[i]+";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
>
>
>
>

Re: how to get the values of checkboxlist in javascript?

am 05.01.2008 11:02:48 von mark

Thanks

"bruce barker" schreef in bericht
news:78414BBD-512E-47F0-8B94-A800264E8F15@microsoft.com...
> 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" wrote:
>
>> 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:
>> ------------------------------------------------------------ -----
>> >> for="vrg2_0">0
>> >> for="vrg2_1">1
>> >> for="vrg2_2">2
>>
>> and this is (part of) the javascript code:
>> --------------------------------------
>> // i=2
>> for (j=0;j<=2;j++)
>> {
>> if (document.getElementById("vrg"+i+"_"+j).checked)
>> {
>> antw[i]=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!)
>> ------------------------------------------------------------ -----
>> >> for="vrg3_0">a
>> >> for="vrg3_1">z
>> >> for="vrg3_2">e
>>
>> and this is (part of) the javascript code:
>> --------------------------------------
>> // i=3
>> for (j=0;j<=2;j++)
>> {
>> if (document.getElementById("vrg"+i+"_"+j).checked)
>> {
>> antw[i]=antw[i]+";"+document.getElementById("vrg"+i+"_"+j).v alue
>> }
>> else
>> { antw[i]=antw[i]+";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
>>
>>
>>
>>