Copy link to clipboard
Copied
I can use event.target.value on the various mouse events, as well as the validate, calculate and format events. However, with event.value I can use this only on the validate, calculate and format events.
For some reason, event.value does not work on the various mouse events.
1. Why doesn't event.value work on the various mouse events?
2. What is the difference between event.target.value and event.value? Both refer to the value in the field whose properties window I am writing my code in.
3. How do you know when to use event.target.value vs. event.value?
Copy link to clipboard
Copied
To summarize, event.value represents the field's new value, while event.target.value represents the field's current value.
The former is not available all the time, only in the events that were triggered by the change of the field's value, like Validation, Calculation, Format and Keystroke. The other events are triggered by the user interacting with the field in some other ways (entering it, exiting it, clicking it, etc.), and not by changing its value, which is why event.value doesn't exist for them.
This can be a bit confusing for some types of fields, like radio-buttons and check-boxes, where clicking them does change their values, and then event.target.value can return either the new value or the old one, depending on at what triggered the event: Mouse Down will return the field's old value, while Mouse Up will return the new value, which was just applied to it by clicking it.
Knowing when to use either one is complicated and depends on your needs. You can read all about the event life-cycle and the meaning of the different types of events in the JavaScript for Acrobat API Reference, but the best way to learn it is through experience.
Copy link to clipboard
Copied
To summarize, event.value represents the field's new value, while event.target.value represents the field's current value.
The former is not available all the time, only in the events that were triggered by the change of the field's value, like Validation, Calculation, Format and Keystroke. The other events are triggered by the user interacting with the field in some other ways (entering it, exiting it, clicking it, etc.), and not by changing its value, which is why event.value doesn't exist for them.
This can be a bit confusing for some types of fields, like radio-buttons and check-boxes, where clicking them does change their values, and then event.target.value can return either the new value or the old one, depending on at what triggered the event: Mouse Down will return the field's old value, while Mouse Up will return the new value, which was just applied to it by clicking it.
Knowing when to use either one is complicated and depends on your needs. You can read all about the event life-cycle and the meaning of the different types of events in the JavaScript for Acrobat API Reference, but the best way to learn it is through experience.
Copy link to clipboard
Copied
Great explanation. This really helps and it makes sense. I reference the SDK PDF quite a bit but I can't always make sense out of it. I do remember reading something that said what is returned by event.value depends on the event that is triggered or something to that effect.
I'll read over your reply a few more times and keep trying things. Thank you very much. This really clears up a lot of things for me.
Copy link to clipboard
Copied
As always Gilad, your knowledge amazes me.
I have a similar question regarding value, valueAsString, and value.toString.
The way I understand valueAsString is it basically only takes the text string out of the field, but I don't understand use-cases or how that differs from value. I don't know much at all regarding value.toString.
Copy link to clipboard
Copied
I think that value.toString() and valueAsString are pretty much the same.
Why should you use valueAsString over value? The value of the field could be a number, or a string, or a boolean, or even an array (if it's a list-box with multiple selected objects, for example). So, if you use the value property to get the values of two fields and then try to add them up, you might get very strange results if one is a string and the other is a number. So it's better to always treat them as strings, and then convert to numbers, if needed.
Copy link to clipboard
Copied
Thank you for the quick response!
That makes perfect sense. I've used both in the past but didn't know the reasoning.

