javascript - Mapping checkbox values to an empty array -
i have series of arrays tied checkboxes.
here html:
<input type="checkbox" value="value1">value 1 <input type="checkbox" value="value2">value 2 <input type="checkbox" value="value3">value 3 in jquery have these arrays defined follows:
var value1 = ['item1', 'item2', 'item3']; var value2 = ['itema', 'itemb', 'itemc']; var value3 = ['item!', 'item@', 'item#']; i want create new array based on of these checkboxes selected , make array of arrays. i've tried following code not working:
var newarray = []; $('input[type=checkbox]:checked').map(function(){ newarray.push($(this).val()); }); i can hard code newarray follows:
var newarray = [value1, value2, value3]; after newarray populated, want loop through values of arrays contained in newarray select random indices , push them randarray. part works if hard code newarray follows:
var newarray = [itemc, item!, item1] but not work if try map checkbox values newarray. once have populated newarray want make sure @ least 1 item each of subsets in newarray included in randarray. part having difficulty getting newarray populated subsets based on checked.
an example of trying accomplish similar found @ http://passwordsgenerator.net/
this syntax not set checkbox value of js variable having name value1:
<input type="checkbox" value="value1">value 1 you might need instead:
<input type="checkbox" value="item1,item2,item3">value 1 <input type="checkbox" value="itema,itemb,itemc">value 2 <input type="checkbox" value="item!,item@,item#">value 3 then newarray creation looks this:
var newarray = []; $('input[type=checkbox]').map(function () { var $cb = $(this); if ($cb.is(":checked")) { newarray.push($cb.val().split(',')); } else { newarray.push([]); } }); see fiddle:
Comments
Post a Comment