jsf 2 - How to get element from javascript in JSF 2.0 - Stack Overflow

I have JSF code like:<h:inputText id="from" value="#{fromToMBean.fromName}">

I have JSF code like:

<h:inputText id="from" value="#{fromToMBean.fromName}"/>

I would like to get this element from JavaScript by ID (from), but I can't, because in generated HTML it is j_idt8:from

How can I get this element in e.g. jQuery? Is there any way to force JSF2 not to change ids?

I have JSF code like:

<h:inputText id="from" value="#{fromToMBean.fromName}"/>

I would like to get this element from JavaScript by ID (from), but I can't, because in generated HTML it is j_idt8:from

How can I get this element in e.g. jQuery? Is there any way to force JSF2 not to change ids?

Share Improve this question asked Mar 26, 2010 at 22:06 amorfisamorfis 15.8k15 gold badges82 silver badges134 bronze badges 2
  • I've answered this question here. – ftravers Commented May 20, 2011 at 7:13
  • possible duplicate of How to select PrimeFaces UI or JSF ponents using jQuery? – BalusC Commented Nov 28, 2012 at 13:40
Add a ment  | 

5 Answers 5

Reset to default 2

You can either use a custom class which you only assign to this element and use css selectors or assign an id to the form and get the element with the id formid:from.

Is there any way to force JSF2 not to change ids?

You can set prependId="false" This way in generated HTML it will be from in place of j_idt8:from.

prependId : Flag indicating whether or not this form should prepend its id to its descendent's id during the clientId generation process. If this flag is not set, the default value is true.

How can I get this element in e.g. jQuery?

You can use ancestorComponent:from in jQuery to get this element.

Actually j_idt8 in j_idt8:from is auto generated id of ancestor ponent of your <h:inputText/>

for example

<h:form id="form">
<h:inputText id="from" value="#{fromToMBean.fromName}"/>
</h:form>

Now generated id of input text would be form:from

If you don't provide id to a ponent than your browser generates that dynamically. So don't forget to provide ids to ponents.

In JSF 1.2 you can use forceId="true". I'm not sure if you can use t:input in JSF 2, but you should be able to. Then it's ID in HTML will be what you expect.

In order to achieve full ID for a ponent, use EL implicit objects and their properties such as #{cc.clientId} and #{ponent.clientId}. Source: Acquire full prefix for a ponent clientId inside naming containers with JSF 2.0.

You can use jquery. Simply, use a selector defining the text it should contains. Something like this:

$( "input[name*='from']" )

'*=' is used to say that the name attribute contains some string. Also there exist '~=' with similar meaning.

For detailed explanations and examples visit http://api.jquery./attribute-contains-selector/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744250514a4565145.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信