Since this sounds like homework - I'll just provide you with some instructions:
- Use a split to separate your comma-delimited list into an array
- Use jQuery's each() method on the array to iterate through the checked
- Then use a $(this).attr('checked',true) on each of those values
Updated with Code and Example:
$(document).ready(function()
{
var stringOfNumbers = "1,3,5,7";
var split = stringOfNumbers.split(',');
$.each(split, function(index, value)
{
$("input[id='"+value+"']").attr('checked',true);
});
});
Function
function CheckTheseBoxes(stringOfNumbers)
{
var split = stringOfNumbers.split(',');
$.each(split, function(index, value)
{
$("input[id='"+value+"']").attr('checked',true);
});
}
Code Explanation:
The stringOfNumbers holds your
comma-delimited string with all of
your numbers to select, the
stringOfNumbers.split(',') separates
all of the values in the string and
stores them in an Array. The $.each()
method iterates through all of the
values in the splitArray (the values
that will determine which checkboxes
are checked.
Inside the loop - a jQuery selector
is built specifying the selection of
an "input" where id is equal to value,
value being the id to select. Finally,
the .attr('checked',true) actually
selects the checkbox.
Working Demo:
Working Demo
Links regarding jQuery and getting started with jQuery
Beginning with jQuery - A Solid Foundation
jQuery.com