Skip to main content
Participating Frequently
April 30, 2016
Answered

How to script a button to clear a field value in one field if another field is null

  • April 30, 2016
  • 3 replies
  • 1731 views

Have a form with fields txtCode.0 to txtCode.20 and txtRow.0 to txtRow.20 created with multiple copies. Want when button butClear is pressed that if field txtCode is null in any rows that the pre-populated value of txtRow is "". 

This topic has been closed for replies.
Correct answer maxwyss

I am still a novice at scripting. Tried the following script:

//Script to remove txtRow numbers with null txtCode field value

var n = 20;

var txtCRR = this.getField("txtCode.").valueAsString;

var txtRRN = this.getField("txtRow.").valueAsString;

for (var i = 0; i <= n; i++)

if (txtCRR.value=="" ||

txtRRN==null)

(txtCRR.value=txtRRN.value)

Unfortunately getting this JavaScript console error:

TypeError: this.getField(...) is null

4:Field:Mouse Up

Any ideas?


Sure, the code as such can not work.

Where does the index come into the game (or, better, the field name)?

The error gets detected in line 4, which means that it occurs befor that, and that would be in line 3.

Does your document have a field with name "txtCode." ???

Because most likely not, the script fails before even executing line 4, because that field does not exist. The error message by Acrobat says that the getField() method returns null. In this context, null has the meaning of not existing.

Back to the original question: As you mention null in the question, there may be a chance that you do not mean "does not exist", but "is empty". Under this assumption, you might try the following:

for (var i = 0 ; i <= 20 ; i++) {

if (this.getField("txtCode." + i).valueAsString == "") {

this.getField("txtRow." + i).value = this.getField("txtRow." + i).defaultValue ;

}

And that should do it…

3 replies

Participating Frequently
May 2, 2016

Just noticed, if I repeat click the button (containing the script) it clears txtRow. after the last populated txtCode. row one row at a time.

Participating Frequently
May 2, 2016

Hit a problem and am wondering if you can help. When I ran the script it looked ok, in that I looked at the first txtCode. null value field and txtRow. was empty. However, in trying test forms the script is only clearing the next row txtRow. value after the last popultated txtCode. field.

I created another form with just these fields and tested it. Script works great. So I know it is not a script issue.

Have you any idea why it is just clearing the first txtRow. value after the last populated txtCode. value, and not the other null txtCode. rows?

Inspiring
April 30, 2016

Do these fields have any formatting being set?

What version of Acrobat are you using?

What is your operating system?

Participating Frequently
April 30, 2016

No formatting, Acrobat Pro DC, Windows 7.

Inspiring
April 30, 2016

Why not just start out with the field's set to an initial value?

Otherwise you need to write some custom JavaScript to clear the fields and then check each field to see it is a null string and if so then set the value of the field.