Changing field values (swap/switch over) using check boxes?

Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

Hi all,

 

Am wondering if anyone knows if this is possible - I have a form with 12 fields, and that allows user entry of a number value anywhere between 0 - 2500. 

 

What I am wondering - is there any way that might be possible to have a user select a 'check box' (or maybe something else if that's not wrokable?) positioned above/adjacent to any 2 of the 12 numeric fields, and then have the respective figures in each numeric field swap places - i.e. If Field 'A' value was "1000" and Field 'B' value was "2000", when checkbox 'A' and 'B' are both ticked, the values in the previously mentioned fields swap places?

 

Of course, this would need to work also for all 12 positions considered, and the various permutations and combinations that are possible (e.g. figure 'A' might need to swap with 'G', maybe 'E' with 'B' etc. - all depends on what two check boxes the user selects!).

 

Thanks...

TOPICS
PDF forms

Views

82

Likes

translate

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

Most Valuable Participant , Jan 27, 2021 Jan 27, 2021
I wrote the code to do it with 4 fields named "Text1" to "Text4" and "Check Box1" to "Check Box4". You can use it as the custom calculation script of one of the text fields:   var max = 4; var checkedBoxes = []; for (var i=1; i<=max; i++) { if (this.getField("Check Box"+i).valueAsString!="Off") { checkedBoxes.push(i); } } if (checkedBoxes.length==2) { var i1 = checkedBoxes[0]; var i2 = checkedBoxes[1]; var f1 = this.getField("Text"+i1); var f2 = this.getField("Text"+i2); var v1 = f2.val...

Likes

translate

Translate

Translate
Most Valuable Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

This is a bit tricky. What about if more than two check-boxes are ticked? Or do you want the script to reset all the check-boxes once the values are swapped?

Likes

translate

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
Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

Hi try67,

 

Yes, did wonder about that (more than 2 selected) and I think a script to reset after the 'swap' has taken place is the answer, so that all tick boxes are empty again (and this would then I believe allow field use again to swap 'back'). Better if i 'PM' you?.

 

Thnaks for the reply....;-)

Likes

translate

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
Most Valuable Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

See my reply below...

Likes

translate

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
Most Valuable Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

I wrote the code to do it with 4 fields named "Text1" to "Text4" and "Check Box1" to "Check Box4".

You can use it as the custom calculation script of one of the text fields:

 

var max = 4;
var checkedBoxes = [];
for (var i=1; i<=max; i++) {
	if (this.getField("Check Box"+i).valueAsString!="Off") {
		checkedBoxes.push(i);
	}
}
if (checkedBoxes.length==2) {
	var i1 = checkedBoxes[0];
	var i2 = checkedBoxes[1];
	var f1 = this.getField("Text"+i1);
	var f2 = this.getField("Text"+i2);
	var v1 = f2.valueAsString;
	f2.value = f1.valueAsString;
	f1.value = v1;
	this.resetForm(["Check Box"+i1, "Check Box"+i2]);
}

 Adjusting the number of fields is done by setting the value of the max variable in the first line.

Likes

translate

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
Participant ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

Hi Try67,

 

That's perfect! (as always, works like a charm....) - thanks yet again ;-).

 

 

Likes

translate

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
Participant ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

Hi Try67,

 

Another question....is it possible to add a second and third 'swapped' lines along with the "Text" fields, e.g. when the check boxes are used, as well as swapping anything entered in the "Text1" thru "Text4" fields, use of the check boxes would also swap a second and third line, called "Ident1" thru "Ident 4" and "Special1" thru "Special4"??

Likes

translate

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
Most Valuable Participant ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

Yes, just duplicate the code that swaps the two TextX fields and rename the fields in it.

Likes

translate

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