How do I go about disabling checkbox selection after so many, in this example 3 is selected
I have tried to coding like this, but it doesn’t do anything
<script type="text/javascript">
function check(){
//var numberOfChecked = $('input[type="checkbox"]:checked').length;
var numberOfChecked = $('input:checkbox:checked').length;
if (numberOfChecked > 3){
$(".cbox").toggle(this.checked);
}
}
Note I removed the onclick attributes from the html and added ‘for’ attributes to the labels so that the checkboxes can be checked easier.
It would probably be best if you told the user they can only select 3 otherwise they might get confused when the last one won’t work. Or alternatively you could disable all other checkboxes when three are selected but would require a little more logic than above.
You realise that sounds quite rude after the effort I put into answering your problem?
You will need to explain what the problem is exactly as it seems to be more or less doing what I expected?
I assumed you only wanted 3 checkboxes to be selected but if there were more checkboxes then they could not be selected unless a previous one was de-selecetd. This is what the code above should be doing as shown in the codepen above.
I did mention that it may be better to disable other checkboxes once 3 had been selected but that would require more logic.
At present you can only ever select 3 checkboxes which I thought was what you wanted? If this is not the case then please clarify what is not working and what you wanted to happen and if your answer is more than 2 words long I may be inclined to answer.
Where did I agree? I wouldn’t agree that I was being “rude” over the internet, how could I? Oh I see, you forgot to put the question mark…looks like we have a troll “adviser”
OK, if you thought it was rude, why did you bother replying? Is there something mentally wrong with people like you to think that people are rude just typing terse statements on the internet? Or do people like you want a whole book worth of explanations of the reason why it doesn’t work when the person doesn’t know why it’s not working?
Why are we arguing here? Paul did a terrific job (as usual, I wish I knew just a 10% of what he knows) and now it works… the thing is… Is it the answer that you were looking for?