I have an array something like
var values = ['1','3','4','5'];
I have the list of checkboxes
<div id='list'>
<input type='checkbox' value='1' />
<input type='checkbox' value='2' />
<input type='checkbox' value='3' />
<input type='checkbox' value='4' />
<input type='checkbox' value='5' />
<input type='checkbox' value='6' />
<input type='checkbox' value='7' />
<input type='checkbox' value='8' />
<input type='checkbox' value='9' />
<input type='checkbox' value='10' />
</div>
now I want to select checkboxes whose values lies within array values. I have done this
for(var i = 0; i < values.length; i++)
$("#list [value=" + values[i] + "]").attr("checked", "checked");
it is working fine but can I do it without for loop.
Thank you in advance to helpers.
$('#CheckAll'). change(function(){ if ($(this).is(":checked")) { $('. checkboxes'). each(function(){ $(this).
Read Multiple Values from Selected CheckboxesUse the foreach() loop to iterate over every selected value of checkboxes and print on the user screen. <? php if(isset($_POST['submit'])){ if(! empty($_POST['checkArr'])){ foreach($_POST['checkArr'] as $checked){ echo $checked.
Try
$("#list").find('[value=' + values.join('], [value=') + ']').prop("checked", true);
Demo: Fiddle
You could do this even more succinctly with a single combined selector:
$('#list [value="'+values.join('"],[value="')+'"]').prop('checked',true);
Which produces a selector like:
$('#list [value="1"],[value="3"],[value="4"],[value="5"]')
http://jsfiddle.net/mblase75/jgqm4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With