Skip to main content
Participating Frequently
March 15, 2024
Question

Only Spawn One Page Determined by Dropdown Selection

  • March 15, 2024
  • 2 replies
  • 1083 views

I'm not a coder and am struggling.  I need the PDF form to spawn a page from templates based on the option selected in the dropdown.  If the user changes their selection, I need the spawned page removed and replaced with a page spawned from the tempalte associated with the new selection.

 

I have a dropdown field and each option corresponds to a template.  The script I have isn't working (nothing happens).

 

Also, is there any way to set the tab order to resume from the dropdown and not move to the spawned page?

 

Any help would be greatly appreciated!!!

 

Here's the code I have in "2 Type" Dropdown properties under Actions/OnBlur/Run a Javascript:

 


var dropdownField = "2 Type"; 
var pageTemplates = {
"Joint": "Template_Joint",
"Trust": "Template_Trust",
"Estate": "Template_Estate",
"C Corp": "Template_Business",
"S Corp": "Template_Business",
"LLC": "Template_Business",
"Sole Proprietorship": "Template_Business",
"Partnership": "Template_Business",
"Traditional": "Template_Traditional",
"Roth": "Template_Traditional",
"Simple IRA": "Template_Simple",
"Self-Employed 401(k)": "Template_SelfEmployed401k",
"Pooled 401(k)/PSP": "Template_Pooled401k",
"UTMA": "Template_UTMA",
"Annuity": "Template_Annuity"
};


this.getField(dropdownField).setAction("OnBlur", function() {
spawnAssociatedPage();
});


function spawnAssociatedPage() {
var selectedValue = this.getField(dropdownField).value;
var templateName = pageTemplates[selectedValue];

// Remove previously spawned pages
removeSpawnedPages();


if (templateName) {
this.spawnPageFromTemplate(templateName);
}
}


function removeSpawnedPages() {
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
if (fieldName.startsWith("Template_")) {
this.removeField(fieldName);
}
}
}

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
March 15, 2024

removeSpawnedPages doesn't delete pages.

Participating Frequently
March 15, 2024

Preach! 😉

try67
Community Expert
Community Expert
March 15, 2024

Is this the only Template you have in the file? If so, you can check the number of pages when a selection is made.

Say the original file has 4 pages, and you always spawn the additional page as the last one. If the file has 4 pages when a selection is made, you spawn the new page. If it has 5 pages you first delete the last page, then spawn.

Participating Frequently
March 15, 2024

Thank yo so much for your reply!

 

Good idea, but there are 10 templates related to this field/script and another field in the document also spawns a different page unrelated to this.  The people filling out the forms won't be able to delete the pages, so I need the script to remove the previously spawned pages when the person changes their selection.

try67
Community Expert
Community Expert
March 15, 2024

In that case it's more complicated. You will need to use some kind of marker to identify those pages, such as a unique field. You then look up that field's page property and use its value to delete the page it's on (and of course I meant that you will do that in your code, not manually by the user).