m benway
m benway

Reputation: 245

XPages csjs pass a variable into a function?

On a button click, I'm calling a function in client side JavaScript.

doIt("TEST");

"TEST" is just the ID of label on my XPage. In the function, I want to use the variable I passed as an ID. Something like:

function doIt(item){
 alert(dojo.query("[id$=':item']").innerHTML);
}

OR

function doIt(item){
 val = XSP.getElementById("#{id:item}").innerHTML;
 alert(val);
}

I have also tried using this, which gives undefined:

val = dojo.query("[id$=':" + item + "']").innerHTML;
alert(val);

If I hard code the ID name like so, then I get the correct innerHTML of the element with the ID "TEST":

val = XSP.getElementById("#{id:TEST}").innerHTML;
alert(val);

Where is my syntax wrong when trying to write this very simple line of code used the passed variable?

Upvotes: 2

Views: 495

Answers (2)

Txemanu
Txemanu

Reputation: 449

In the "onclick" client-event in the button, (inside XPage or CC), the client Ids can be computed, so you should put there something like this:

doIt("#{id:Test}");   // << "#{id:Test}" is computed in the server-side and the final client ID is sent to the browser

Then, in your cjs library (cjs libraries are not "evaluated" before sending to the client, so here you cannot use "#{id:Test}" expressions) you should have something like:

function doIt(idElement) {
    var domElem = dojo.byId(idElement);  // << here you get the element
}

Upvotes: 0

Knut Herrmann
Knut Herrmann

Reputation: 30960

The easiest way is to call your function with the complete id:

doIt("#{id:Test}")

and to use it in your function

function doIt(item){
    alert(dojo.byId(item).innerHTML);
}

Upvotes: 1

Related Questions