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

Button to increase/decrease number of grouped checkboxes filled in sequence, plus increase a counter

Community Beginner ,
Mar 24, 2022 Mar 24, 2022

Copy link to clipboard

Copied

I'm trying to make a button on either side of a row of 20 checkboxes that, when pressed, will either increase or decrease the checked boxes in sequence by 1 (ie: Pressing the "+" button would increase the number of checked boxes from 0 to 1, 1 to 2, etc). As well, I have a separate counter (text field) that would gain or lose a number respectively with each button press (ie: Pressing the button does the above function, but also increases the counter from 0 to 1, or decreases from 1 to 0, etc).

I think I have an idea of how it might be done, but i'm not familiar enough with grouping commands to know if it can be done in sequence like that - without putting in repeated lines of "look at each individual box and see which is active". (If it comes to that, it comes to that).

Here's what I think i'd have to do for each checkbox to get it to read their state:

if (this.getField("Damage 1").value !="Off")
this.getField("Damage 1").checkThisBox(0,false) {

} else {

this.getField("Damage 1").checkThisBox(0,true)

}

TOPICS
How to , JavaScript , PDF forms

Views

632

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Mar 30, 2022 Mar 30, 2022

There is something wrong with the field.

You have two option:

1. Delete field and create new one.

2.Create new field and give it same name and hide it, and put scripts in that field.

Here is file where I implement option 2 (I had to choose option 2 because I don't have that font) for you to test it:

https://drive.google.com/uc?export=download&id=1_Srszw4lPm0z6QL4yFF0GSnYcSVlaqqk 

If you want field to be empty if it's 0 use this as validation script:

if(event.value==0)event.value="";

I did that i

...

Votes

Translate

Translate
Community Expert ,
Mar 24, 2022 Mar 24, 2022

Copy link to clipboard

Copied

I assume your checkboxes are named "Damage 1-20" and text field is named "counter", if not, rename fields, you can use this:

At '+' button  Action tab -> Mouse UP -> Run a JavaScript

for(var i=1; i<=20; i++){
if(this.getField("Damage "+i).valueAsString == "Off"){
this.getField("Damage "+i).checkThisBox(0,true);
this.getField("counter").value += 1;
break;}}

 

At '-' button  Action tab -> Mouse UP -> Run a JavaScript

for(var i=20; i>=1; i--){
if(this.getField("Damage "+i).valueAsString != "Off"){
this.getField("Damage "+i).checkThisBox(0,false);
this.getField("counter").value -= 1;
break;}}

 

Votes

Translate

Translate

Report

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 Beginner ,
Mar 25, 2022 Mar 25, 2022

Copy link to clipboard

Copied

This worked like a charm! However, now that i've got the counter's font set up, i've run into another snag: the way it's set, I'd like leading 0's in front of all single digits. Right now it only displays, for example, "1, 2, 3, etc", when i'd like it to display "01, 02, 03, etc" when it adds to it. Otherwise the code works splendidly, I can't thank you enough for your help!

Votes

Translate

Translate

Report

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 25, 2022 Mar 25, 2022

Copy link to clipboard

Copied

At "counter" field properties -> format tab -> custom -> as custom format script use this:

if(event.value){
if(event.value.length == 1 && event.value != 0)
event.value = "0"+event.value;}

Votes

Translate

Translate

Report

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 Beginner ,
Mar 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

Sorry for the late reply - I was out for the weekend - but that custom formatting doesn't seem to stay. I put in the code on that page, but it doesn't display it and is blank when I go back into it. I'm unsure if this is an issue overall or just with this text field.

Votes

Translate

Translate

Report

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 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

Can you share your file?

Votes

Translate

Translate

Report

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 Beginner ,
Mar 30, 2022 Mar 30, 2022

Copy link to clipboard

Copied

Yep! I'm sure it's something in the background I'm unaware of that's causing it.

Votes

Translate

Translate

Report

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 30, 2022 Mar 30, 2022

Copy link to clipboard

Copied

There is something wrong with the field.

You have two option:

1. Delete field and create new one.

2.Create new field and give it same name and hide it, and put scripts in that field.

Here is file where I implement option 2 (I had to choose option 2 because I don't have that font) for you to test it:

https://drive.google.com/uc?export=download&id=1_Srszw4lPm0z6QL4yFF0GSnYcSVlaqqk 

If you want field to be empty if it's 0 use this as validation script:

if(event.value==0)event.value="";

I did that in test file.

Votes

Translate

Translate

Report

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 Beginner ,
Mar 31, 2022 Mar 31, 2022

Copy link to clipboard

Copied

LATEST

I had figured something was up with it. That's excellent, I really do appreciate the help with this! It turned out to be a bit more complicated than I was expecting, haha. Many thanks!

Votes

Translate

Translate

Report

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