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

change button label

Participant ,
Oct 06, 2023 Oct 06, 2023

Hi
I would like to insert a function in the validation script of a field called "age" that allows me to change the label of a button called "Button5"
I tried to build this function
if(event.value>45){var s1=75-event.value;this.getField("Button5").buttonSetCaption(this.getField("Button5").buttonGetCaption()+s1);}
else{var s1=30;this.getField("Button5").buttonSetCaption(this.getField("Button5").buttonGetCaption()+"30");}

This feature works well when the button label is blank. The problem arises when there is already a number in the button label.
In fact, if the button label is empty and I enter the number 50 in the "age" field, the number 25 appears in the button label.
If I subsequently change the number in the "age" field and enter, for example, the number 40, 2530 appears in the button label and not 30 as I would like.

Please, can anyone help me solve this problem?
Thank you

TOPICS
JavaScript
1.6K
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 ,
Oct 06, 2023 Oct 06, 2023

Use:

Number(this.getField("Button5").buttonGetCaption()) + s1

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 ,
Oct 06, 2023 Oct 06, 2023

Also, remove the quotes around the numbers.

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
Participant ,
Oct 07, 2023 Oct 07, 2023

Hi
It does not work
This is the custom validation script of the "age" field

if(event.value>45){var s1=75-event.value;Number(this.getField("Button5").buttonGetCaption()+s1);}
else{var s1=30;Number(this.getField("Button5").buttonGetCaption()+30);}

If I delete the button label and enter the number 40 in the "age" field, the number 30 appears in the button label.
Then, if I change the number in the "age" field and write for example 50, the number that appears in the button label is 3025 and not 25 as I would like

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 ,
Oct 08, 2023 Oct 08, 2023

Why have you removed

 this.getField("Button5").buttonSetCaption

Use this:

var s1;
if (event.value>45) s1 = 75-event.value;
else s1 = 30;
this.getField("Button5").buttonSetCaption(Number(this.getField("Button5").buttonGetCaption())+s1);
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 ,
Oct 08, 2023 Oct 08, 2023

The main issue with your code is that you're continually adding values to the button's caption, so each time you change the value of the age field, the button's caption will increase in value.

What exactly are you trying to achieve with this code? What do you want the button to show?

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
Participant ,
Oct 08, 2023 Oct 08, 2023

I'm trying to ensure that if I write a number greater than 45 in the "age" field, a number equal to the difference between the number 75 and the number written in the "age" field must appear in the label of the "Button5" button.
However, if the number written in the "age" field is less than or equal to 45, the number 30 must appear in the label of the "Button5" button.

Now I'll start from the beginning.
I clear the button label "Button5" and clear the "age" field.
Then I insert Bernd Alheit's custom validation script into the "age" field.
I write the number 50 in the "age" field and the number 25 appears in the label of the "Button5" button. Correct
Then, I change the number in the "age" field and write the number 40, the number 55 appears in the label of the "Button5" button. Wrong. The result is incorrect because the number 30 should appear.

Then, I also tried this script
var ageField = this.getField("age");
var buttonField = this.getField("Button5");
// Check if the "age" field contains a valid value
if (!isNaN(etaField.value)) {var etaValue = parseFloat(etaField.value);
// Calculate the value to insert into the button label
var s1 = (etaValue > 45) ? 75 - etaValue : 30;
// I set the button label with the calculated value
buttonField.buttonSetCaption(s1.toString());}

I repeat the previous operation:
1) I clean the "age" field and clean the Button5 button label
2) I insert the function I wrote above into the custom validation script.

when I write the number 50 in the "age" field,
the number 30 appears on the label of the "Button5" button.
I cross out the number 50 in the "age" field and write the number 40,
the number 25 appears on the label of the "Button5" button.
I cross out the number 40 in the "age" field and write the number 60,
the number 30 appears on the label of the "Button5" button.
I cross out the number 60 in the "age" field and write the number 20,
the number 15 appears on the label of the "Button5" button.

That is, when I change the number in the "age" field, the result of the previous operation appears in the label of the "Button5" button.

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 ,
Oct 08, 2023 Oct 08, 2023

At the custom validation script of the "age" field use:

var s1;
if (event.value>45) s1 = 75-event.value;
else s1 = 30;
this.getField("Button5").buttonSetCaption(s1);
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
Participant ,
Oct 08, 2023 Oct 08, 2023

Works!!!
Thank you

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
Participant ,
Oct 08, 2023 Oct 08, 2023

I rewrote it like this
(event.value>45)?s1 = 75-event.value:s1=30;this.getField("Button5").buttonSetCaption(s1);

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 ,
Oct 08, 2023 Oct 08, 2023
LATEST

A better format is this:

var s1 = (event.value>45) ? 75-event.value : 30;

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