Skip to main content
deckarduk
Inspiring
April 2, 2021
Question

Adding checkboxes with same name but different export values using JS

  • April 2, 2021
  • 1 reply
  • 1001 views

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. 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
April 2, 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;
deckarduk
deckardukAuthor
Inspiring
April 2, 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.

try67
Community Expert
Community Expert
April 2, 2021

No, JS has no access to that info.