Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Javascript for any one of multiple fields to set a flag/variable

Avatar

Level 5

I need help creating a script that will read through several fields, and if any one of them meets a condition (let's say, "Yes"), then Checkbox1 is checked.

The pseudocode would be:

If Field1 is Yes

     or If Field2 is Yes

     or If Field3 is Yes

     or If Field4 is Yes

then Checkbox1 = checked

So, a "Yes" one of those fields generates the check in the box.

The other part of this is if someone were to set Fields 1-4 to "Yes" and then back to "No",  I need a script that checks all four fields and if they are all "No", then sets Checkbox1 to unchecked.

I can't code, but I know how to edit/add existing script.  I really appreciate any help.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi,

Maybe something like this JavaScript code in the calculate event of the checkbox field

 

if (Field1.rawValue == "No" && Field2.rawValue == "No" && Field3.rawValue == "No" && Field4.rawValue == "No")

{

   this.rawValue = "0";   

}

else

{

    if (Field1.rawValue == "Yes" || Field2.rawValue == "Yes" || Field3.rawValue == "Yes" || Field4.rawValue == "Yes")

  {

       this.rawValue = "1";

  }

    else

  {

      this.rawValue = "0";

  }

}

 

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Hi,

Maybe something like this JavaScript code in the calculate event of the checkbox field

 

if (Field1.rawValue == "No" && Field2.rawValue == "No" && Field3.rawValue == "No" && Field4.rawValue == "No")

{

   this.rawValue = "0";   

}

else

{

    if (Field1.rawValue == "Yes" || Field2.rawValue == "Yes" || Field3.rawValue == "Yes" || Field4.rawValue == "Yes")

  {

       this.rawValue = "1";

  }

    else

  {

      this.rawValue = "0";

  }

}

 

Avatar

Level 5

Great, thanks!  I have two questions, I hope you don't mind:

1. Why does one if-statement have "&&" and the other has "||"?

2.  It seems like both conditions are already taken care of in the first two if/else statements.  What is the final "else" statement for?

Avatar

Level 5

I think I figured out the "or" vs. "and" function of && and ||.  So the only question remains about the final "else" statement.  Thanks.

Avatar

Level 10

Hi,

You may not need the else, I wasn't sure what other values the fields might have than Yes or No.

Regards

Bruce