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
  • 100,159
  • 46
  • 371
  • 480
dido
  • 2,330
  • 8
  • 37
  • 54

4 Answers4

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

dknaack
  • 60,192
  • 27
  • 155
  • 202
  • I would [avoid using `new Array`](http://stackoverflow.com/questions/4292048/why-use-instead-of-new-object-and-use-instead-of-new-array-and-true-fa) – Chad Jan 23 '12 at 14:01
  • 1
    +1 to this, i had almost the identical solution written in my jsfiddle. Curse you, time! – Anders Arpi Jan 23 '12 at 14:02
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);
    }
})();
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
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/

Sagar Patil
  • 1,938
  • 13
  • 21
-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');
                }
             });
        }
    }
  • Best solution ever! hope it help u.! – Ouch Bros Dec 07 '16 at 02:53
  • 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. – Stargateur Dec 07 '16 at 03:24