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

Adding checkboxes with same name but different export values using JS

Contributor ,
Apr 02, 2021 Apr 02, 2021

Please can someone help me.

I'm trying to add several checkboxes to a page.

All the checkboxes should have the same name but have different export values.

Is this possible as so far I've not succeeded.

Here's the code so far...

thisField = this.getField('dupethis');

var cRtn = app.response("How many checkboxes would you like?");
var prevRect = thisField.rect;
var prevName = thisField.name;
//this.removeField(theFieldName);
var offset = 30;

for (var i = 0; i < cRtn; i++) {
var newRect = [prevRect[0]+offset, prevRect[1], prevRect[2]+offset, prevRect[3]]
var fn = this.addField({
					cName: "p1f1" + "_CB",
					cFieldType: "checkbox",
					nPageNum: 0,
					oCoords: newRect
					});
offset = offset + 30;
var thisExpVal = "sammy" + offset;
fn.exportValues=[thisExpVal];
}


I'm having problems because I'm trying to give all the checkboxes the same name.

I need this so the checkboxes then behave like radio buttons, i.e. only one option can be selected, however with set up the user can uncheck all of the checkboxes.

 

Is this possible? 

Thanks in advance. 

TOPICS
Acrobat SDK and JavaScript , Mac
823
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 ,
Apr 02, 2021 Apr 02, 2021

You should apply the export values after having added all of the check-boxes. Try this code:

 

thisField = this.getField('dupethis');

var cRtn = app.response("How many checkboxes would you like?");
var prevRect = thisField.rect;
var prevName = thisField.name;
//this.removeField(theFieldName);
var offset = 30;

var exportVals = [];
for (var i = 0; i < cRtn; i++) {
	var newRect = [prevRect[0]+offset, prevRect[1], prevRect[2]+offset, prevRect[3]]
	this.addField({
		cName: "p1f1_CB",
		cFieldType: "checkbox",
		nPageNum: 0,
		oCoords: newRect
	});
	offset = offset + 30;
	var thisExpVal = "sammy" + offset;
	exportVals.push(thisExpVal);
}
var fn = this.getField("p1f1_CB");
fn.exportValues=exportVals;
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
Contributor ,
Apr 02, 2021 Apr 02, 2021

Thanks for the speedy help Gilad. 

That works perfectly 🙂

 

Please can I check, rather than referring to the field like this...

thisField = this.getField('dupethis');

 ... is there no way of referring to a 'selected' checkbox when editing in 'Prepare Form'?

 

Thanks again for the help.

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 ,
Apr 02, 2021 Apr 02, 2021

No, JS has no access to that info.

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
Contributor ,
Apr 02, 2021 Apr 02, 2021
LATEST

Thanks for the confirmation.

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