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");

12 comments:

  1. dojo.query("[name$=#{id:optionName}]:checked").length>0

    ReplyDelete
  2. For Checkbox Group client side js try:

    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(',')); //Returns all checked values of the check box group

    ReplyDelete
  3. Hi Celina Insurance Developers,
    Thank a lot . I was searching for this. really helped me..saved my time

    ReplyDelete
  4. I am using checkbox group in xpages and the checkbox group value is computed. I tried all the above methods to retrieve the checked values from checkbox but nothing worked. I used the folowing method : getComponent(“Checkboxlist”).getAttributes().values() and it returns empty array. Anyone has any better ideas ? Please help

    ReplyDelete
  5. This may help: http://stackoverflow.com/questions/9871872/how-can-i-get-the-checkbox-groups-label-value

    ReplyDelete
  6. getComponent("ControlID").setValue("something");

    i am not getting the setValue while using the getComponent, i am getting teh getValueBinding.

    ReplyDelete
  7. How to loop to get values from field whose ID are as Field_1, Field_2...Field_15

    Need it urgently

    ReplyDelete
    Replies
    1. Hello. I would try posting your question on StackOverflow using the XPages tag: https://stackoverflow.com/questions/tagged/xpages.

      You could also try posting your question on the XPages forum: http://www-10.lotus.com/ldd/xpagesforum.nsf.

      Delete
  8. For Setting value in List Box CSJS:

    var listbox = document.getElementById("#{id:Facility}");
    for (var i=0; i<listbox.options.length; i++){
    if (listbox.options[i].value == "10" || listbox.options[i].value == "15") {
    listbox.options[i].selected = true;
    } else {
    listbox.options[i].selected = false;
    }
    }

    ReplyDelete