Jump to content

All other checkboxes==false possible?


gaijin

Recommended Posts

I'm hoping somebody will be able to set me on the right path to making my code simpler, and I hope my description will suffice.
Here is my query:
When I use many checkboxes on a page, I'd like to know if there is a way to make the array so that I only need to specify the 'true' values, and have a blanket for all other boxes to be 'false'.
In this following example it is easy to get the desired result to get 'message 1' when boxes 1 and 4 are checked, but 2 and 3 remain unchecked; and also 'message 2' then boxes 1, 2 and 4 are checked but box 3 remains unchecked, and then 'message 3' for all other combinations:
{
if(cb[1].checked==true&&cb[2].checked==false&&cb[3].checked==false&&cb[4].checked==true) { alert('message' 1); }
else if(cb[1].checked==true&&cb[2].checked==true&&cb[3].checked==false&&cb[4].checked==true) {alert('message' 2); }
else { alert('message 3'); }
}
But if I have 12 boxes, with a very large total of possible combinations, it would be impractical to use that string, as I need to specify the majority of combinations.
And if I simply use:
{
if(cb[1].checked==true&&cb[4].checked==true) { alert('message' 1); }
else { alert('message 3'); }
}
then the user gets 'message 1' even if he checks, say, box 5 along with boxes 1 and 4.
So, I'd like to know if there is a way to require 'all other boxes' to be unchecked.
Thanks in advance for any ideas.
Link to comment
Share on other sites

a simple way is to first run a for loop over the array. store a count of how many boxes are true. then run the code with one extra test like so:

var checkCount = 0;for(var i =0;i<cb.length;i++){     checkCount += cb[i].checked;}if(checkCount == 2   && cb[1].checked   && cb[4].checked){     alert('message' 1);}else{     alert('message' 3);}

This works only if you're making tests where a checkbox must be true.

 

if you really want to get advanced and theres a large number of combinations (where a box must be true,false,or either) you could create an array of objects defining their conditions and attached message. and then iterate via condition group instead of checkbox:

function cMessage(condMess,defaultMessage){  //define all possible conditions and their messages.   //previously defined conditions have higher priority over later conditions.if(typeof condMess == "undefined")  condMess = [   {conditions:{1:true,2:false,3:false,4:true},    message:"'message' 1"},   {conditions:{1:true,2:true,3:false,4:true},    message:"'message' 2"},   {conditions:{1:true},    message:"'message' 4"}             ];  //default message to return if none of the conditions are met  if(typeof defaultMessage == "undefined")    defaultMessage = "'message' 3";  for(var i =0;i<condMess.length;i++)  {     var valid = true;     for( var j in condMess[i].conditions)    {      valid = valid && cb[j].checked == condMess[i].conditions[j];     }    if(valid)    {      return condMess[i].message;    }  }  return defaultMessage; }

 

in a condition you can say that a box (say box 1 for example) must be checked (set to true), must NOT be checked (set to false), or the box's value doesn't matter (not even mentioned). for instance in the 3rd condition that will print out message 4 its ignoring checkboxes 2,3, and 4 since they haven't been mentioned.

 

in this case since its checking for the trues and falses you need to be a little more explict and define the expected values for each box per message.

Edited by Hadien
  • Like 1
Link to comment
Share on other sites

Thanks, much appreciated. As I'm not too good at javascript it'll take me a while, and some tinkering, to absorb your information!

 

Update:

Thanks Ingolme and Hadien for your replies. It seems there's no easy way, but your code, Hadien, is certainly shorter than the one I had been working with, and where you commented in "//previously defined conditions have higher priority over later conditions." it looks like that could be useful for me.

Edited by gaijin
Link to comment
Share on other sites

Hadien, well I was probably a bit understating it when I said I wasn't very good at this... your first example looks just right for me, but could you tell me what to put after the opening script tag? I haven't been able to get it to work so far.

Link to comment
Share on other sites

Most likely from the misplaced single quotes in those alerts. there's no operator in-between the message string and the number, so JavaScript might be growing an unexpected/ILLEGAL token error. If you have access to a JavaScript debugger, it should help you find what problems are occurring on the page and what error your getting from my code. In most browsers, they come with built-in debuggers, which is normally activated by pressing f12

Link to comment
Share on other sites

Yes, I'd already spotted that, but I don't have quotes in the actual test page.

 

Chrome says 'Uncaught ReferenceError: cb is not defined'

 

What do I put in the form onsubmit= ?

On the page I've already been using I have onsubmit="doRedirect(this);return false;"> but I don't know how to adapt it to your code.

 

sorry to ask such basic questions.

Link to comment
Share on other sites

That error is happening because of one of two things, you define cMessage in one scope and cb in a different scope, or because in your code you don't call cb "cb", but some other variable. Functions have access to any variable defined inside, with, or beside it. But they don't have access to variables defined in scopes they are not a part of:

var globalscope = "foo and everyone else can see me";function foo(){var fooscope ="only foo can see me";alert(globalscope);alert(fooscope);alert(barscope);}function bar(){var barscope ="only bar() can see me";}foo();//will alert the first two scopes and then error when trying to alert a third// this is because barscope wasn't defined in a scope that foo() has// access to and thus has no idea what it is. Both foo and bar know about// global scope.
Try to think of scopes as inheritance. A function will have access it's own scope, and it's parent scope, grandparent scope, etc.etc. But it will not have access to it's siblings, aunts/uncles, nefews, or even it's own children. Foo and bar are siblings so while they share the same parent scope, they cannot see each other's scope (they can see and call each other, but they have no idea what inside their sibling). To solve the error you have you can do one of two things, either put cMessage in a scope that knows what cb is (or whatever you called cb and make sure it's has the right name in the function), or you can update cMessage to have an extra argument to where you pass in the cb array. I'm guessing that doRedirect() is a function that you use to validate something and if it passes you make an Ajax call. You can call cMessage here (if you're using it to validate something). Since you haven't posted any code, I can't 100% tell exactly what's wrong or how you need to code something relative to what you have. I can only make assumptions here.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...