Copy link to clipboard
Copied
I'm trying to make a condition, if one of the 15 comboboxes have a certain listed text value selected, then a button will appear.
I tried to follow this example I found here on another question:
var t = document.getElementById("fTerminalType");
var selectedText = t.options[t.selectedIndex].text;
so the part of my code that uses it is:
for (i=1;i<16;i++) {
var t = this.getField("ComboBox"+i);
var s = t.options[t.selectedIndex].text;
if (s=="Specific Text") {
k.display = display.visible;
}
}
So `Combobox1` has "Specific Text" selected, but the `k` button does not display visible , and the rest of the code (a calculation) returns empty.
If I remove the internal code from `for () {}`, the rest of the calculation works, so the syntax error is specific to the text comparison part.
I tried `.value`,`.innerHTML`,`.export.value`,`.change` and `.changeEx` instead of `.text`, nothing worked.
I don't know if pdf forms have jQuery, I could try it but would need further help with the code either way.
Thanks.
The value property will return the export value of the selected item if it has one, otherwise it will return whatever is displayed, which is what you're calling text. SO if someone types something, you can retrieve it with the value property, although you really should use valueAsString instead. Check the JavaScript console (Ctrl+J) to see if any errors are reported.
If you need to get the display value from the export value, you can use the currentValueIndices field property to get the index of
...Copy link to clipboard
Copied
You're mixing up browser-based JavaScript with PDF-based JavaScript, so it needs some revision. Are you saying that this code is included in a field's custom calculation script, or something else? It important to know when you want this code to be triggered.
Copy link to clipboard
Copied
Hi, yes the code is meant to be in a custom calculated field. the calculation has many conditions, one of them is "having one combobox with the specific text selected".
Turns out I tried it in a much simpler way, testing in another textfield
var CB = this.getField("ComboBox");
event.value = CB.value
the event field transcribed the value (which is the same as the text)
And transcribing it to my original calculated field, at least it returns the calculation as before, so I guess I fixed the syntax
One question remains: if I had to have the text, not the value, how would I do it? I tried .text in the testing textbox, it returned "undefined". I have no idea what that actually means.
Copy link to clipboard
Copied
What do you mean by text, and how is it different than the selected value? Are you using export values for the combo box items? The result of "undefined" means that you tried to get the value of a non-existent property, in this case .text.
Copy link to clipboard
Copied
Yes i'm using export values, but that is generated when I populate the list of the combobox. Suppose the user types in the combobox, that is a text and not a value, isn't it? if they are always the same then no problems I guess.
So for my original calculation, following the syntax used in the test textbox that did return the content of the combobox:
for (i=1;i<16;i++) {
var t = this.getField("ComboBox"+i);
if (t.value=="Specific Text") {
k.display = display.visible;
}
}
The problem is even now the rest of the calculation is bugged and the result is blank... is there still anything wrong there?
Copy link to clipboard
Copied
The value property will return the export value of the selected item if it has one, otherwise it will return whatever is displayed, which is what you're calling text. SO if someone types something, you can retrieve it with the value property, although you really should use valueAsString instead. Check the JavaScript console (Ctrl+J) to see if any errors are reported.
If you need to get the display value from the export value, you can use the currentValueIndices field property to get the index of the selected item and use it with the getItemAt field method to get the display value. Some sample code is in the Acrobat JavaScript reference.
I don't notice anything wrong with your updated script, though you should add a break statement after the k.display statement since you don't need to keep looping once it's set. It's not clear if it's initialized somehow to display.hidden, which would make sense.
Copy link to clipboard
Copied
Thanks George for being thorough and patient. I'm new to all this and although I Will have to read the JS reference and guides to really understand all your examples (and I will, because I may need them later).
For this question, specifically, the console showed me the error...
Remember I said the field was a calculation? and it was working without the conditional. But it wouldn't work with the conditional because one of the other ifs (not pasted here) had the same value as event.value (which was the result of the calculation)... I switched event.value to a f.value (f being associated with the very field I'm coding in..) and it all debugged.
The string for example, I've seen it in many case examples but didn't quite get it, how does it help in this case? if this it too tutorial material, please just point me to a 101 read... i have downloaded a bunch of guides but am honestly quite overwhelmed and not much of a clue how to search stuff yet.
About the break statement, that's probably good, but how do I add it? after the "k.display=display.hidden;"
should I just add a "break;" right before the "}" that closes the loop?
Also, if it's not asking too much, could you please take a look at my other question, made in the javascript subforum? (How do I add any typed text in a combo-box to its own dropdown list?)
Thanks again!
Copy link to clipboard
Copied
Yes, add the break statement as you described.
Regarding using valueAsString vs value to when getting the value of a field, you should always use it when you later intend to use the value as a string. It sounds like you have the combo box set up to allow the user to "Allow user to enter custom text". If they were to enter a numeric value with leading zeroes (e.g., 001234556), the value property would return a number (not a string) of 1234556, not the string "001234556" as you get when using the valueAsString property. If a user were to enter "true", the value property will return a boolean value of true, not the string "true". If you were to then use a string property/method on either of these values (e.g., length property or replace method), you will get an error or bug.
var s1 = 1234556; // Number
s1.length // returns undefined
var s2 = "001234556"; // String
s2.length // returns 9
var s3 = true; // Boolean
s3.replace("e", ""); // generates TypeError: s3.replace is not a function
var s4 = "true"; // String
s4.replace("e", ""); // returns the string "tru"
Although these examples may seem contrived, the point is if you're going to use the field value as a string, it's a good idea to get into the habit of using the valueAsString property.
I'll be able to look at your other question later...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now