Copy link to clipboard
Copied
I am extremely new to JavaScript.
I have 2 different text fields named, item and quantity. I want those 2 fields to be filled out in order to be able to fill out the comments field.
I know I need an if statement. I don't know where it is supposed to go. I tried putting it in the custom calculations script, but I'm not even sure if that's right. I know the script is not even right. If someone could help me out I would greatly appreciate it. But could you also explain the answer in detail.
Also is read only the only way you can deactivate/disable a field. Is there a way to make it so you cannot click in the field?
if (event.value=="" && this.getField("Quantity").value="";)
this.getField("Comments").readonly = false;
else
this.getField("Comments").readonly = truse;
Yes, but it's not different from the other code... However, you should be using valueAsString instead of value.
Try this:
if (this.getField("Item Code").valueAsString.length > 0 && this.getField("Quantity").valueAsString.length > 0) {
event.target.readonly = false;
} else {
event.target.readonly = true;
}
Copy link to clipboard
Copied
You have several errors in the code. Also, I would move the code to the custom calculation script of the Comments field. It can be something like this:
if (this.getField("Item").valueAsString=="" || this.getField("Quantity").valueAsString="") {
event.target.readonly = true;
event.value = "";
} else {
event.target.readonly = false;
}
I also added a command to reset the field when it is set as read-only, you'll notice. Otherwise whatever the user entered into it will remain there, and they won't be able to edit it any longer...
Copy link to clipboard
Copied
Thank you for your fast response. However I am getting a error message. It says "invalid assignment left-hand side". And it highlights this line . I am not familiar with this message
event.target.readonly = true;
Copy link to clipboard
Copied
I tried posting a reply but it seems to have been lost in the ether... Anyway, you can use this code as the custom calculation script of the Comments field to do it:
if (this.getField("Item").valueAsString=="" || this.getField("Quantity").valueAsString="") {
event.target.readonly = true;
event.value = "";
} else {
event.target.readonly = false;
}
Copy link to clipboard
Copied
Use the script as custom calculation script of the "Comments" field, something like this should work fine:
if (this.getField("Quantity").value !=="") event.target.readonly = false;
else event.target.readonly = true;
open the "Prepare Form", select "Properties " from the context menu and see slide below to see where you need to place this line of code:
Copy link to clipboard
Copied
Sorry again, try67. Seems like I'm always crashing into the discussions almost at the same time as you. I didn't see you already answered.
Copy link to clipboard
Copied
Thank you for your fast reply. But this this equation take into account that both fields need to be empty? I appreciate the visuals as well.
Copy link to clipboard
Copied
Then like this:
if ((this.getField("Item").value !=="")&&(this.getField("Quantity").value !=="")) event.target.readonly = false;
else event.target.readonly = true;
Copy link to clipboard
Copied
I entered your equation like you said and it doesn't save in the custom calculations. I click edit, enter the equation, and click save and it remains blank.
Copy link to clipboard
Copied
I made a mistake, but try67 code is the correct answer.
You original inquiry stated:
So that means the custom calc script needs a little tweak.
Out of my respect towards try67, I would let him handle the rest of the conversation.
I consider him my mentor in JavaScript scripting, and it is mostly from his direct guidance that I am able to contribute with some spontaneous creativity.
It wasn't my intention to jump in in this discussion like that.
Copy link to clipboard
Copied
Thank you for your help. I am learning from both of you so I greatly appreciate it
Copy link to clipboard
Copied
Okay, combining both of your equations and using the validation section instead of the calculation section, I was able to enter the following code:
if ((this.getField("Item Code").value !=="")&&(this.getField("Quantity").value !=="")) {
event.target.readonly = true;
event.value = "";
} else {
event.target.readonly = false;}
However it makes the field read-only all of the time. Does anyone know what is wrong? It should only be readable if text is entered into both fields.
Copy link to clipboard
Copied
It seems fine... Are there any error messages in the JS Console after you change the value of one of the fields?
Copy link to clipboard
Copied
I don't see an error in the JS console.
But I was wondering if I could do something like this. Since there needs to be something entered into both the item and quantity fields in order for the comments section to be readable, then could I use something like this? I get a syntax error, but could it work?
if ((this.getField("Item Code").value.length > 0)&&(this.getField("Quantity").value.length > 0))) {
event.target.readonly = false;
} else {
event.target.readonly = true;}
Copy link to clipboard
Copied
Yes, but it's not different from the other code... However, you should be using valueAsString instead of value.
Try this:
if (this.getField("Item Code").valueAsString.length > 0 && this.getField("Quantity").valueAsString.length > 0) {
event.target.readonly = false;
} else {
event.target.readonly = true;
}
Copy link to clipboard
Copied
It worked! Thank you. One question, why should you use valueasstring? When should you use that versus value
Copy link to clipboard
Copied
The value property can return a value of various types: string, number, boolean or even an array (object, technically). That means comparing it to another value (like an empty string) can fail. If you use valueAsString the result will always be a string, so you could safely compare it to another string and know you're comparing apples to apples, not apples to oranges. Also, the value property is read/write, while valueAsString is read-only, so you can't accidentally overwrite the original value if you make a mistake using it.