1

I has checkbox the list of media in my webpage

<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaA
<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaB
<input name="c1"  type="checkbox" value=<%= item.MediaId %> />> mediaC

In my javascript,How can i keep the checkbox value inside the array for example

if user check for mediaA and mediaC in array will be {12,14}

Thanks ^_^

Poomjai
  • 51
  • 5
  • possible duplicate of [Select values of checkbox group with jQuery](http://stackoverflow.com/questions/416752/select-values-of-checkbox-group-with-jquery) – mmmmmm May 03 '14 at 12:22

1 Answers1

0

If jQuery is an option, you can get an array of the values using .map() like this:

var vals = $("input[name=c1]:checked").map(function(i, cb) {
  return $(cb).val();
}).get();
alert(vals);

vals would contain an array of the values of the checked checkboxes, e.g. [12, 14] if A and C were checked in this:

<input name="c1"  type="checkbox" value="12" /> mediaA
<input name="c1"  type="checkbox" value="13" /> mediaB
<input name="c1"  type="checkbox" value="14" /> mediaC
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155