Skip to main content
January 12, 2017
Answered

Clear text field when checkbox is unchecked

  • January 12, 2017
  • 1 reply
  • 7085 views

Hello,

I'm trying to write some JavaScript for new forms at work.  I've been able to write a code so that when a checkbox is checked, the information from one set of fields copies over to another:

if(getField("Check Box12").value!="On")

this.getField("Applicant's Name").value = this.getField("Property Owners Name").value;

this.getField("pertyAddress").value = this.getField("Address").value;

this.getField("Contact Name1").value = this.getField("Contact Name").value;

this.getField("Phone1").value = this.getField("Phone").value;

this.getField("Postal Code1").value = this.getField("Postal Code").value;

this.getField("Cell Phone1").value = this.getField("Cell Phone").value;

this.getField("Email1").value = this.getField("Email").value;

What I'm trying to do now is if that checkbox is unchecked, have the text fields clear and reset to blank.  Right now I'm trying to use this and it is not working:

if(getField("Check Box12").value!="False")

this.resetForm("Applicant's Name")

this.resetForm("pertyAddress")

this.resetForm("Contact Name1")

this.resetForm("Phone1")

this.resetForm("Postal Code1")

this.resetForm("Cell Phone1")

this.resetForm("Email1")

Any suggestions?

TIA!

This topic has been closed for replies.
Correct answer try67

Couple of notes.

- To execute more than one line of code after an if-statement you have to use curly brackets around the whole code block.

- The default value of a check-box that isn't ticked is "Off", not "False".

- You can reset multiple fields in a single command by putting then in an array as the parameter of the resetForm method.

- I believe you defined your conditions the other way around. The "!=" operator means NOT EQUALS. You probably want to use the EQUALS operator, which is "==".

So combining it all your code should be something like this:

if (this.getField("Check Box12").value=="Off") {

    this.resetForm(["Applicant's Name", "pertyAddress", "Contact Name1", "Phone1", "Postal Code1", "Cell Phone1", "Email1"]);

} else {

    this.getField("Applicant's Name").value = this.getField("Property Owners Name").value;

    this.getField("pertyAddress").value = this.getField("Address").value;

    this.getField("Contact Name1").value = this.getField("Contact Name").value;

    this.getField("Phone1").value = this.getField("Phone").value;

    this.getField("Postal Code1").value = this.getField("Postal Code").value;

    this.getField("Cell Phone1").value = this.getField("Cell Phone").value;

    this.getField("Email1").value = this.getField("Email").value;

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
January 12, 2017

Couple of notes.

- To execute more than one line of code after an if-statement you have to use curly brackets around the whole code block.

- The default value of a check-box that isn't ticked is "Off", not "False".

- You can reset multiple fields in a single command by putting then in an array as the parameter of the resetForm method.

- I believe you defined your conditions the other way around. The "!=" operator means NOT EQUALS. You probably want to use the EQUALS operator, which is "==".

So combining it all your code should be something like this:

if (this.getField("Check Box12").value=="Off") {

    this.resetForm(["Applicant's Name", "pertyAddress", "Contact Name1", "Phone1", "Postal Code1", "Cell Phone1", "Email1"]);

} else {

    this.getField("Applicant's Name").value = this.getField("Property Owners Name").value;

    this.getField("pertyAddress").value = this.getField("Address").value;

    this.getField("Contact Name1").value = this.getField("Contact Name").value;

    this.getField("Phone1").value = this.getField("Phone").value;

    this.getField("Postal Code1").value = this.getField("Postal Code").value;

    this.getField("Cell Phone1").value = this.getField("Cell Phone").value;

    this.getField("Email1").value = this.getField("Email").value;

}

January 12, 2017

Thank-you very much! That worked perfectly.