Saturday, January 24, 2009

How to generate a forced unique id in JSF

In JSF (Java Server Faces), we may face a scenario where we have to refer a element like textarea or input text box by unique id (as an example use document.getElementById in Javascript).

But as JSF has its own convention of generating ids, the id cannot be determined unless we use forceId="true" as an attribute as in the example below
 <h:inputTextarea id="questiondesc" forceId="true"  ....more attributes here

As this text area is within a form whose id is qtype
<h:form name="qtype" id="qtype" enctype="multipart/form-data">
we have to refer it in JavaScript as below.
var tarea=document.getElementById("qtype:questiondesc");