views:

87

answers:

3

Hi, I have been working on a page that will allow entry into a certain part of my website if the user selects 8 out of 25 checkboxes in the right sequence.

Here is what I have working so far CLICK HERE TO SEE A LIVE VERSION

My question is, how can I completely disable the rest of the checkboxes after 8 have been chosen, so far I am using javascript to keep count, and I have an alert popup keeping them from selecting any more, but I would like to completely disable the rest of the checkboxes if possible.

A: 

You can do this by modifying the setChecks method as follows:

function setChecks(obj) {
    if(obj.checked) {

        checkCount = checkCount + 1;

        //note: modify this selector to specify the actual table in question,
        //of course. If this is not known beforehand (ie there are multiple
        //tables of checkboxes), you can use
        //  $(this).closest('table').find('input:checkbox:not(:checked)')
        //instead.
        if(checkCount == 8)
            $('table input:checkbox:not(:checked)').attr('disabled', 'disabled');

    } else {

        if(checkCount == 8)
            $('table input:checkbox:not(:checked)').removeAttr('disabled');

        checkCount = checkCount - 1;
    }
}

Unrelated to the question at hand, but hopefully helpful: I know this is a jsfiddle example and thus probably not indicative of your actual code, but you should consider using consistent formatting, especially with regard to indentation (and to a lesser extent spacing). Also, it's good habit to always use semicolons when writing javascript, even though they are sometimes optional. Readable code is much easier to debug, extend, and definitely easier for others to understand.

Another thing you can do to simplify your code is use stylesheets instead of specifying width and align in every td element, and using unobtrusive javascript instead of the onclick event in every checkbox. Those can all be replaced by a simple jQuery bind statement in your document.ready event:

$('table input:checkbox').click(setChecks);

This would require modifying setChecks to receive an event parameter instead. (Edit) Like so:

function setChecks() {
    if(this.checked) {
        ...
    } else { ... }
}

You don't actually need the event parameter because this refers to the current event target, so you can just remove that parameter altogether.

Ian Henry
@Ian: you could be more specific with your selectors are you're going to end up selecting all checkboxes inside any table.
Moin Zaman
@Moin: Very true; I was in the process of editing my answer when you posted that.
Ian Henry
Yeah, my first time trying javascript was about a day and a half ago. i have a long way to go. Im gonna try the code you gave also the advice given. Also, I was also thinking about the onclick situation, I never did like doing it that way, in fact i think with a little modification of your code, I can get rid of setchecks code altogether by sticking parts of you code into the doument ready code. hope Im making sense
James
@Ian, I have no clue where semi colons are needed, but I will definately look into that, as well as the formatting. As far as the cell heights and width, Im using dreamweaver for the html part, i plan on attempting to make the code neater overall once I get something working.
James
@Ian Im looking into the setchecks code above, how would I add an event parameter to setchecks?
James
Cool, thank you so much for your help!
James
A: 

where you have the alert right now, use this bit of jQuery:

$('input[name=checkbox]:not(:checked)').attr('disabled','disabled');

See a fork of your code with this working: http://jsfiddle.net/fKwEQ/

Moin Zaman
seems simple enough, I have one question, how would I account for if a user unchecks a box.
James
A: 

I have simplified your code and it's here.

and this will answer your question,

    //update checkCount 
    checkCount = $(':checked').length;

    if (checkCount >= maxChecks) {
        //alert('you may only choose up to ' + maxChecks + ' options');
        $(':checkbox[name=checkbox]').not(':checked').attr('disabled', true);
    } else {
        $(':checkbox[name=checkbox]:disabled').attr('disabled', false);
    }
Reigel
Thanks :) Just what I needed!
James