I have the folowing inptuts!
<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" />
After using jQuery I want my result to look like this:
<input type="checkbox" value="1" checked />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" checked />
<input type="checkbox" value="4" />
<input type="checkbox" value="5" checked />
I use following code, but it doesn't work.
$(document).ready(function() {
var str = '1,3,5';
var temp = new Array();
temp = str.split(",");
for (a in temp ) {
//$('.check').valueOf().checked;
$('.check').val(append(temp[a])).checked;
//$('#c').append();
}
});
Thanks in advance!
You can loop through your array using for(i=0;i!=temp.length;i++)
and use the attribute selector to get the right checkbox.
Check out my sample and this jsFiddle Demonstration
$(document).ready(function() {
var str = '1,3,5';
var temp = new Array();
temp = str.split(",");
for (i=0; i!=temp.length;i++) {
var checkbox = $("input[type='checkbox'][value='"+temp[i]+"']");
checkbox.attr("checked","checked");
}
});
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