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

wildcard statement?

Avatar

Level 4

I have 6 buttons controlling 6 identically sized  text boxes contained in a single subform.

If the user clicks on button 1, textbox 1 is unhidden. If button 2 is clicked, textbox 2 is unhidden and textbox 1 is hidden etc etc.

I'm presently using an "if else" statement to hide and unhide textboxes like this:

if (button.rawValue == "1")
    {
    TextField1.presence = "visible";
    }
else
    {
    TextField1.presence = "hidden";
    TextField2.presence = "hidden";
    TextField3.presence = "hidden";
    TextField4.presence = "hidden";
    TextField5.presence = "hidden";
    TextField6.presence = "hidden";
    }

What I'm wondering is if there is some way of saying

if (button.rawValue == "1"), all other items in this subform are hidden

Is that possible with JS?

Graham

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Graham

If you are willing to change your text fields so they have the same name there are then a number of ways you can refer to them as a group, so if your subform containing the buttons and text fields was called SubForm and the text fields were all called TextField then then "SubForm.TextField.all" will return a list of all the text fields.

Then if you have a function like;

function set(subForm, index)
{
var textFields = subForm.TextField.all;
for (i = 0; i < textFields.length; i++)
{
  if (textFields.item(i).index == index)
  {
   textFields.item(i).presence = "visible";
  }
  else
  {
   textFields.item(i).presence = "invisible";
  }
}
}

And in each button click call the function with;

set(SubForm, this.index);

The function will make visible the text field that has the same index as the button that was clicked.

Bruce

View solution in original post

4 Replies

Avatar

Former Community Member

Just hide the subform .....subformName.presence = "invisible"

Paul

Avatar

Level 4

Paul,

But I am trying to always show one of the 6 textboxes inside the subform.

I guess what I'm trying to say is if there's some way to cut down on the lines of code, or am I always going to have to say something to the effect of

"if line one is visible,

then hide line 2

hide line 3

hide line 4

hide line 5

hide line 6"

I'd like to say

"if line one is visible,

then hide all else"

That would be a lot less code to write.

Graham

Avatar

Correct answer by
Level 10

Hi Graham

If you are willing to change your text fields so they have the same name there are then a number of ways you can refer to them as a group, so if your subform containing the buttons and text fields was called SubForm and the text fields were all called TextField then then "SubForm.TextField.all" will return a list of all the text fields.

Then if you have a function like;

function set(subForm, index)
{
var textFields = subForm.TextField.all;
for (i = 0; i < textFields.length; i++)
{
  if (textFields.item(i).index == index)
  {
   textFields.item(i).presence = "visible";
  }
  else
  {
   textFields.item(i).presence = "invisible";
  }
}
}

And in each button click call the function with;

set(SubForm, this.index);

The function will make visible the text field that has the same index as the button that was clicked.

Bruce

Avatar

Level 10

Nice bit of code Bruce.

I was giving this problem some thought, and figured something could be done with a loop  but my javascript-fu still needs some work.