Skip to main content
Participant
May 21, 2021
Question

Changing Text Field size with Javascript based on Conditions

  • May 21, 2021
  • 1 reply
  • 2633 views

Hi everyone,

 

I have a text field "exteriorElectricalDeficiency1" that causes a second text field "exteriorElectricalPrice1" to become hidden one the word "NOTE" or "note" is written in it. 

 

I'm trying increase the current width of the deficiency text box of '6.5748 in' to '7.2112 in' as long as the price field is hidden and back to its original size when it isn't hidden. 

 

This is my current code attempting to accomplish this: 

//This hides the price field if the deficiency contains a note
if(event.value.indexOf("note") > -1 || event.value.indexOf("NOTE") > -1) {
    defNumber = event.target.name.substring(event.target.name.length - 1);
    this.getField("exteriorElectricalPrice" + defNumber).hidden = true;
    if (this.getField("exteriorElectricalPrice" + defNumber).hidden == true) {
        event.target.w = "7.2112in"; 
        //I've also tried event.target.x = "..."
    } 
} else {
    this.getField("exteriorElectricalPrice" + defNumber).hidden = false; 
    event.target.w = "6.5748in";
    //I've also tried event.target.x = "..."
}

 

This code is in the validation script of the deficiency box. 

I'm not sure if the '.x' or the '.w' should be used and I also don't know if the 'event.target' should be used.

 

Any input you guys can give would be amazing!

 

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
May 21, 2021

First, read this article:

https://www.pdfscripting.com/public/PDF-Page-Coordinates.cfm#FldLocate

 

The Field size is determined by the fields "rect" property.

Here's the reference entry:

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FField_properties.htm%23TOC_rect3bc-37&rhtocid=_6_1_8_31_1_36

 

Your script has a couple of issues and there are several things you could do to improve it.

Fist, "defNumber" can't be used if it is never defined. So it needs to be moved out of the "if".  Also, the price field is used in all cases so might as well get that up front. For improvements, use a regular expression to detect the word "Note". And use the character index in the string to acquire the field number.  

 

var defNumber = event.target.name[event.target.name.length - 1];
var  priceFld = this.getField("exteriorElectricalPrice" + defNumber);
if(/note/i.test(event.value) {
   priceFld.hidden = true;
   // code to increase current field rectangle;
} else {
    priceFld.hidden = false; 
    // code to decrease current field rectangle;
}

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often