tags:

views:

63

answers:

1

Hello, I want to dynamically create object of HtmlDivElement in my jsf managed bean and add it to panel but it seems that HtmlDivElement is interface. So, how can i do it?

+5  A: 

This is a pretty major confusion. The org.w3c.dom.html.HTMLDivElement is not a JSF component. This represents a W3 DOM element which has an entirely different purpose (JAXP, DOM parsing).

You need a subclass of javax.faces.component.UIComponent (just click your way through the "Direct Known Subclasses" in the aforelinked Javadoc to find them all). To render a HTML <div> element, just use HtmlPanelGroup whose layout attribute is set to block.

HtmlPanelGroup div = new HtmlPanelGroup();
div.setLayout("block");
someParentComponent.getChildren().add(div);

which does effectively the same as the following in "static" JSF:

<h:panelGroup layout="block" />
BalusC
thanks BalusC! but now i have another problem. The id's that i give to dynamically generated panelgroup doesn't look same when i view source in mozilla i.e if i give id as "abc" + autoincrementNo, then when i do view source in mozilla i get something like j_idt9:abc_1. How do i get rid of this j_idt9: which automatically gets prefixed. Actually it's important for me to get control on generated id's else i wouldn't have asked such silly question :p
Ankit Rathod
You need to give each of the parent `UINamingContainer` components (`f:subview`, `h:form`, `h:dataTable`, etc) a fixed ID as well. BTW: if you actually need fixed ID's for JavaScript reasons, then there are other (better) ways to solve this, under each just passing the element itself to the JS function as `this`. E.g. `onclick="doSomething(this)"` and `function doSomething(element) { var id = element.id; }`
BalusC
By the way I see in your profile that you never upvoted nor downvoted. An answer with 0 score does look weird. You can upvote by pressing the up arrow and downvote by pressing the down arrow. When you like any answer, upvote it. When the answer doesn't have any value, just leave it so. When you dislike any answer, downvote it.
BalusC
Ok thanks Balcus,but now again if i give my form a id as frm now the id changes to frm:abc_1? This will actually do my job because there are no numbers in prefix but still for information can you tell me how do i get completely rid of it?I know passing this concept in javascript, but actually i am using jquery portlet and it has little awkward requirement, it needs id's as name_number.And thanks for informing. I, actually was not aware of that! Now i will take care of this.
Ankit Rathod
Hey sorry! i got it myself. i forgot that form has property prependid. I set it to false and now it's perfectly fine :) Thanks BalusC
Ankit Rathod
In jQuery you can also just use the "Attribute Ends With Selector" http://api.jquery.com/attribute-ends-with-selector/ e.g. `$('[id$=abc1]`)`.
BalusC