Javascript code for a PDF Form
- November 7, 2022
- 2 replies
- 2933 views
Greetings,
I'll start by saying that I'm not a computer coder by any means, however I have been able to search and almost find a solution to my task. I'm not quite there yet and I don't understand enough of it to figure it out unfortunately. I don't think what I'm trying to accomplish is very difficult either. Here is my setup.
I have a pdf form with three columns. st1 + st2 + st3 = ttm(hidden cell) which is then converted to HH:MM in tt1 with this javascript code.
var v = Number(this.getField("ttm").valueAsString);
var hours = Math.floor(v/60);
var minutes= (v%60);
var minutesString = (minutes<10) ? "0"+minutes : ""+minutes;
event.value = hours + ":" + minutesString;
That script works, however it is one step behind in calculations. The ttm is updated whenever a value is entered in stx but the javascript code won't update unless something else is changed in the form. The whole point of creating the hidden (ttm) was as a work around since I couldn't figure out the sum of fields in javascript and then assign that sum to var v. The javascript code should ideally calculate that sum and then apply the code to convert it into HH:MM format.
The next part of the form is column 3, Part 91 y1,y2,y3), which ideally would be a checkmark or I can make it into a text field. The goal for that column is if a check mark or value is entered then that row would not be included in the calculation for tt1.

I have this working code that does what I want it to do, I just don't know how to modify it to incorporate it into my pdf form.
var st1 = 60;
var st2 = 20;
var st3 = 50;
var y1 = '';
var y2 = '';
var y3 = 'y';
var v = 0;
for (x=1; x<4; x++) {
if (eval("y"+x).length === 0) {
v += eval("st"+x);
}
}
var hours = Math.floor(v/60);
var minutes = (v%60);
var minutesString = (minutes<10)? "0"+minutes: ""+minutes;
var final = hours + ":" + minutesString;
console.log(final);I've included the sample pdf that I've referenced so that it can be viewed in case my explanations were a bit vague. Any assistance would be greatly appreciated.
Thanks,
