tags:

views:

38

answers:

3

Hi guys,

Basically all I'm trying to do is if one check box is selected by the user, then automatically check the previous box. Each check box has an iterating number id. So for example if checkbox id=3 is selected then auto select check box id=2. Here's my code below:

<script>   
var $j = jQuery.noConflict();

$j('input:checkbox').click(function()
{   
    if (this.checked)
    {
        var original = $j(this).attr("id");
        var temp = original - 1;
        var follower = $j(temp);

        follower.attr("checked",true);
    }
});
</script>

This doesn't do anything at all. Can someone help tell me what's wrong or if there's a better way to do this that would be awesome! Thanks!

+1  A: 

I think all you need to do is change:

follower.attr("checked",true);

to:

follower.attr("checked","checked");
Ender
Coronus is correct, I missed that the hash sign was missing from the selector.
Ender
+3  A: 

How about

var follower = $j('#' + temp);
follower.attr('checked','checked');
Coronus
+2  A: 

Few suggestions (indicated in the comments):

<script>   
var $j = jQuery.noConflict();
// wrap this code in a "DOM ready" handler
$j(function() {
    // listen to change event instead (to support keyboard driven interaction)
    $j('input:checkbox').change(function() {   
        if (this.checked) {
            // '+' char will cast this to a numerical  value
            var original = +$j(this).attr("id");
            var temp = original - 1;
            // be sure to use an id selector
            var follower = $j("#" + temp);
            // more convential way of marking item as checked
            follower.attr("checked","checked");
        }
    });
});
</script>


edit: just noticed that @Coronus and @Ender beat me to the selector and checked additions... so credit to where it's due :)

jmar777