XPages Tip: Getting the Value of the Current Field with SSJS (@ThisValue equivalent)
Sometimes, I miss the convenience of several @functions. If you’re writing SSJS code that needs to access the value of the current field from an event handler on that field, but you don’t want to hard-code the component name (so you can easily reuse it), it would be handy if there was an @ThisValue equivalent. Alas, there isn’t, but, fortunately, you can still easily access the value.
this keyword is where we need to start. It provides a handle to the current object.
When running code from an event handler, the object type is:
print (typeof this);
The event handler doesn’t have a value — we need to get to the parent component that contains it. That’s where the
getParent() method comes in handy. As you would expect, it goes up the chain and gives us a handle to the parent object, which for my sample input field is:
This object has a
getValue() method to return the value of the component.
Getting the Current Field Value
Putting this together, I can easily get the value with this line:
var thisValue = this.getParent().getValue();
FYI — if you want the ID of the parent control from an event handler, you can use this line:
var thisID = this.getParent().getId();