4

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!

Leniel Maccaferri
103k48 gold badges381 silver badges495 bronze badges
asked Jan 23, 2012 at 13:52

4 Answers 4

7

Description

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

Sample

$(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");
 }
});

More Information

answered Jan 23, 2012 at 13:58
Sign up to request clarification or add additional context in comments.

2 Comments

+1 to this, i had almost the identical solution written in my jsfiddle. Curse you, time!
1

I must ask, how exactly do you expect that to work? For starters you're calling for elements with the class name check, which none of your elements have, then you're just having a statement without any assignment or anything... You're also creating an array that just gets thrown away because split creates a new array. Further, instead of your string splitting, just something like var temp = [1,3,5]; would work fine... And finally you use for...in on an array, which is bad because arrays have properties beside their keys (such as length and several methods)...

Anyway, what you need is something more like:

(function() {
 var elms = document.querySelectorAll('input[type=checkbox]'), l = qsa.length, i;
 for( i=0; i<l; i++) {
 elms[i].checked = (elms[i].value % 2 == 1);
 }
})();
answered Jan 23, 2012 at 13:56

1 Comment

This is reliant on him only ever wanting odd numbers to be checked, isn't it? Seems sort of risky.
1

Try this:

$(document).ready(function(){
 $("input[type='checkbox']").each(function(){
 if( $(this).index() % 2 == 0 ){
 $(this).attr('checked','checked');
 }
 });
});

You can play around with the fiddle here - http://jsfiddle.net/EuXQF/

answered Jan 23, 2012 at 14:07

Comments

-1
$(document).ready(function(){
 var array = "1,3,5,2";
 check_value(array);
});
 function check_value(val){
 var array = val.split(",");
 for (i=0;i<array.length;i++){
 //alert(array[i]);
 $("input[type='checkbox']").each(function(){
 if( $(this).val() == array[i]){
 $(this).attr('checked','checked');
 }
 });
 }
 }
answered Dec 7, 2016 at 2:52

2 Comments

Best solution ever! hope it help u.!
your post is a little bit strange, thank, if you want help. But it looks like a spam. The question has been open 4 years ago. There was already some good answer. And you don't need to comment your answer in that way. I encourage you to answer the hot question with no accepted answer. When you get used to stack overflow, don't hesitate to answer any question that you think it's needed.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.