0

We can get element by static ID using rich:clientId() function as follows

document.getElementById('#{rich:clientId(JSF_ID)}').click(); 

However, I need to use a dynamic ID which takes the form of var + "_ID" where var can be employee, student, etc and thus resulting in employee_ID, student_ID as actual ID.

I tried as follows:

dynamicID = var + '_ID';
document.getElementById('#{rich:clientId(dynamicID)}').click();

However, it didn't work. How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • 1
    rich:clientId is evaluated by server. if you check the generated js code, you will see that. – Johny T Koshy Dec 02 '13 at 08:49
  • 1
    Please specify the requirement you're trying to achieve the wrong way. As stated in MrD's answer and in johny's comment, there is no way to run JSF-related artifacts in the client side. So, you either have to establish a way of finding the components without resorting to the server-side finders, or attach all proper finders on the server so that all of the JavaSscipt functions are already dealing with proper component ids. – skuntsel Dec 02 '13 at 08:55
  • @BalusC you assume that dynamicID = 'anyText' + '_ID'; any solution ? – Ahmed Nabil Dec 03 '13 at 11:31

2 Answers2

0

It looks like you are setting dynamicID via JavaScript, am I right? If yes, the EL-expression #{rich:clientId(dynamicID)} cannot be evaluated as EL on the server during the rendering of the page since the dynamicID is only available on the client (=browser) when the page has already been built up on the server and sent to the browser.

Where does var come from? How is it applied to the component with the dynamicID? Can't you use the same approach for the getElementById?

MrD
  • 1,255
  • 1
  • 10
  • 24
0

For concatenating a variable with a constant in JSF on server-side, see the following already answered post Concatenate strings in JSF/JSP EL and Javascript. Basically it gives you a how-to in creation of an own concatenation function for jsp/jsf. Works pretty need.

As a hint: you can use #{rich:element(JSF_ID)} instead of document.getElementById('#{rich:clientId(JSF_ID)}') to keep the source readeable - it will render identical results.

Good luck...

Community
  • 1
  • 1
L-Ray
  • 1,637
  • 1
  • 16
  • 29