Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looping backwards through Array setting checkbox defaults depending on separate values

Explorer ,
Mar 01, 2021 Mar 01, 2021

I have an array of checkboxes called slots and a text field called level. I want the number of checkboxes checked by default to equal the number in level. I made this function.

function SetSlots()
{
for (i=0;i<level.value;i++)
{this.getField(slots[i]).defaultIsChecked(0, true);}
};

this works when increasing the value of 'level' but I did not define the conditions for changing the default to unchecked. I figure I need a way to define the amount of boxes checked by default (x) and compare them to 'level'.

if (x > level.value){};

 Now I need a way to uncheck the boxes according to the difference between x and level and moving in reverse so I made this function.

function RemoveSlots()
{
for (i=0;i<x-level.value;i--)
{this.getField(slots[i]).defaultIsChecked(0, false);}
};

The final script I am trying to use

SetSlots()
else if (x>level.value)
RemoveSlots();

The issues I am having are in properly defining the number of boxes checked by default. Currently working with this in the document level

var x = 0;
for(i=0; i<slots.length; i++){
if(this.getField(slots[i]).defaultIsChecked(0,true))x++};

It does not return anything except 0 so I know I have something wrong here.

My second issue is with the RemoveSlots() function only unchecking the 1ist box.
I know I have something wrong but I can't figure out what. As always, any help is always appreciated!
Thanks for reading.

TOPICS
Edit and convert PDFs , JavaScript , PDF forms
792
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Explorer ,
Mar 01, 2021 Mar 01, 2021

I had an aha moment. The remove function doesn't work because I had the loop start at the beginning not the end.  Also the difference I needed was between the total number of boxes and the level.value. Seeing that I was able to fit it all in one function 

 

function SetSlots()
{
for (i=0;i<level.value;i++)
{this.getField(slots[i]).defaultIsChecked(0, true);}
for (i=level.value;i<22-level.value;i++)
{this.getField(slots[i]).defaultIsChecked(0, false);}
};

 

Hopefully that will help someone with part of the issue. I am still curious as to why the checked variable returns only zero.

 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 01, 2021 Mar 01, 2021

I had an aha moment. The remove function doesn't work because I had the loop start at the beginning not the end.  Also the difference I needed was between the total number of boxes and the level.value. Seeing that I was able to fit it all in one function 

 

function SetSlots()
{
for (i=0;i<level.value;i++)
{this.getField(slots[i]).defaultIsChecked(0, true);}
for (i=level.value;i<22-level.value;i++)
{this.getField(slots[i]).defaultIsChecked(0, false);}
};

 

Hopefully that will help someone with part of the issue. I am still curious as to why the checked variable returns only zero.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 01, 2021 Mar 01, 2021

- Where do you define the level and slots arrays?

- defaultIsChecked doesn't actually change anything, it just returns a value about the current state of the group.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2021 Mar 03, 2021

I have those variables at document level along with the function. I call the function in a longer conditional equation attatched to a text field. As of right now the function does make the checkboxes I want checked by default. If there is a more stable or simple way of doing this, I would love to learn it. I am trying to teach myself Javascript so I understand just because it works doesn't mean it's right.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 03, 2021 Mar 03, 2021
LATEST

Ah so my issue with the checked returning zero is because I wasn't phrasing it correctly

maybe

var x = 0;
for(i=0; i<slots.length; i++){
if(this.getField(slots[i]).defaultIsChecked(0,true))x++};

should be 

if (this.getField(slots[i]).value !=="Off")x++}; ?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines