admin管理员组文章数量:1356966
I would like to use JavaScript to manipulate hidden input fields in a JSF/Facelets page. When the page loads, I need to set a hidden field to the color depth of the client.
From my Facelet:
<body onload="setColorDepth(document.getElementById(?????);">
<h:form>
<h:inputHidden value="#{login.colorDepth}" id="colorDepth" />
</h:form>
When JSF processes the page, it is of course changing the IDs of the elements. What's the best way to reference these elements from my JavaScript code?
I would like to use JavaScript to manipulate hidden input fields in a JSF/Facelets page. When the page loads, I need to set a hidden field to the color depth of the client.
From my Facelet:
<body onload="setColorDepth(document.getElementById(?????);">
<h:form>
<h:inputHidden value="#{login.colorDepth}" id="colorDepth" />
</h:form>
When JSF processes the page, it is of course changing the IDs of the elements. What's the best way to reference these elements from my JavaScript code?
Share Improve this question asked Oct 23, 2008 at 20:31 Eric NoobEric Noob 1,4913 gold badges15 silver badges14 bronze badges4 Answers
Reset to default 4You'll want to set the ID of the form so you'll know what it is. Then you'll be able to construct the actual element ID.
<body onload="setColorDepth(document.getElementById('myForm:colorDepth');">
<h:form id="myForm">
<h:inputHidden value="#{login.colorDepth}" id="colorDepth" />
</h:form>
If you don't want to set the form's ID field, you could find it at runtime, like so:
<body onload="setColorDepth(document.getElementById(document.forms[0].id + ':colorDepth');">
View the generated html source and look at what the jsf named the id attribute of the tag.
You will soon see how the naming convention works. Its usually like FORMNAME:FIELDNAME
You can use the control's clientId as returned by UIComponent.getClientId(FacesContext). See here for sample code.
Define a function findElement globally and use it everywhere
function findElement(elementId) {
if(document.getElementById(elementId)) return elementId;
for(var i = 0; i < document.forms.length; i++) {
if(document.getElementById(document.forms[i].id + ':' + elementId)) {
return document.forms[i].id + ':' + elementId;
}
}
return null;
}
<body onload="setColorDepth(findElement('colorDepth'));">
本文标签: Using JavaScript with JSF and FaceletsStack Overflow
版权声明:本文标题:Using JavaScript with JSF and Facelets - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743953474a2567713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论