Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Hide all subforms using a for loop function

Avatar

Level 4

Hi,

I have a basic check that shows or hides a subform based on if a checkbox has been checked or not.

if (checkbox.rawValue == 1) {

this.presence = "visible";

}

else {this.presence = "hidden":

}

Now, this checkbox controls the visibility of multiple subforms, so I would like to have a function on a script object that I can call from each subform's calculate event.

I named the script object "Common".  At first I tried just an if/then statement such as above, but "this" is meaningless in the function. 

So I've resorted to creating a loop.  I've tried to make a test loop to hide all subforms, but it's not working.   Once I get it to work, I can add an if/else statement that will hide currentElement if the checkbox is checked but for the sake of simplicity I haven't gotten to that point.

Any tips?  I derived this loop from a check-for-errors loop example from Niall O'donovan.

function HideSubforms(myParentObject) {

 

          // Declare some variables

          var allChildElements;

          var intNumElements;

          var currentElement;

          var i;

 

          // Get all the child nodes of the parent element

          allChildElements = myParentObject.nodes;

          // Total number of elements in the object

          intNumElements = allChildElements.length;

 

          // Loop through all the child elements

          for (i=0; i<intNumElements; i++) {

                    currentElement = allChildElements.item(i);

 

                    if(currentElement.className === "subform") {          

                              currentElement.presence = "hidden";

 

                    }

}

} // End of the function

1 Accepted Solution

Avatar

Correct answer by
Level 10

You need to pass the root subform as an argument to this function.

      

  Common.HideSubforms(this.parent.parent);

   

In my screenshot, I was calling this function on the initialize event of the TextField1 inside Subform2.

I need to pass the rootnode (Page1) to this function to work.

Untitled.jpg

Thanks

Srini

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

You need to pass the root subform as an argument to this function.

      

  Common.HideSubforms(this.parent.parent);

   

In my screenshot, I was calling this function on the initialize event of the TextField1 inside Subform2.

I need to pass the rootnode (Page1) to this function to work.

Untitled.jpg

Thanks

Srini

Avatar

Level 4

Srini,

I have one follow up question. If I only want a single subform to be hidden (and not its parent), how can I do that?  The reality of my form is that the subforms I want hidden are not all within the same parent, and other sibling subforms will have a different script.

I tried to call it using these ways:

Common.HideSubforms(this);

Common.HideSubforms(this.nodes);

I'm not really sure what to do here; I could just wrap the needed subform in its own subform (bumping it "down" in the heirarchy) but then that negates the ease and convenience of using this script.