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
The special 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: com.ibm.xsp.component.xp.XspEventHandler
I find this by using the typeof operator in JavaScript
print (typeof this);
getParent()
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: com.ibm.xsp.component.xp.XspInputText
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();
