XPages Tip: Get an element’s id outside XPages events with CSJS

I was facing a problem today where I tried to set a value and do a partial refresh within a client script block on my XPage. Since this script block is outside any XPage event, I could not use the following SSJS within my CSJS to get the IDs of the elements I want to set and refresh:

dojo.byId(„#{id:idOfMyComponent}“);

I googled a bit but was not able to find a solution for this problem. There are many hints but you need to bring them together to get the result. Here is the code that finally worked for me:

var idOfElementToSetValueTo= “;
var idOfElementToCallPartialRefreshOn= “;
dojo.query(„[id$=\\:elementID]“).forEach(function(node, index, arr){
                         idOfElementToSetValueTo = node.id;                           
                   });;
dojo.query(„[id$=\\:refreshID]“).forEach(function(node, index, arr){
                    idOfElementToCallPartialRefreshOn= node.id;                           
                   });;

 dojo.byId(idOfElementToSetValueTo).value=strFilterVal;
XSP.partialRefreshPost(idOfElementToCallPartialRefreshOn,{});

The only downside is that if there are more components on the page all ending with the same „:id“, the function provided will only return the last element. You might want to extend the function to suit your needs then.

Hope this helps!