Apr 7, 2011

Getting / Setting Values with SSJS and CSJS

Below are some methods we have come across for getting and setting values in XPages. This list is an ongoing process. Please comment if you see something that isn't listed.

SSJS - Server Side JavaScript

Getting SSJS Values (getting values with SSJS require the data to be submitted. These snippets will return 'null' if the data has not been updated/submitted to the server)
------------------------------------ 
Edit Box, Check Box, Radio Button, Radio Button Group:
getComponent("ControlID").value

Check Box group:
...


Setting SSJS Values
------------------------------------
Edit Box:
getComponent("ControlID").setValue("something");


______________________________________________


CSJS - Client Side JavaScript

Getting CSJS Values
------------------------------------
Edit Box, Combo Box, Radio Button:
XSP.getElementById("#{id:ControlID}").value

Check Box:
XSP.getElementById("#{id:ControlID}").checked  //will return 'true' if checked, 'false' if not checked

Check Box Group:
var checkField=document.getElementsByName("#{id: ControlID }");
var result=[];
for(var i=0;i<checkField.length;i++){
if(checkField[i].checked){
result.push(checkField[i].value);
}
}
alert(result.join(','));

Radio Button Group:
var result=null;
for(var i=0; i<document.forms[0].elements.length; i++){
    if(document.forms[0].elements[i].name=="#{id:ControlID}" ){

        if(document.forms[0].elements[i].checked == true){
            result=document.forms[0].elements[i].value;
            break; //remove this if for check box groups and collect multiple values above instead
}}}
alert(result); //result is the currently selected value of the radio group


Setting CSJS Values
-----------------
Edit Box:
XSP.getElementById("#{id:ControlID}").value = "something";

Radio Button Group:
function setRadioValue(id, value)
{
    var elements = document.getElementsByName (id);
    for(i=0;i<elements.length;i++) {
        if (elements[i].value == value) {
            elements[i].checked = true;
        }
    }
}
setRadioValue("#{id:radioGroup}", "value");