Programmatically change an HTML attribute value.
Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by a div element. The div element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on the div element that would replace everything within the div. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate the div element, then a modification of its 'class' attribute. Alternately, if I am required to create the div element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Programmatically change an HTML attribute value.
AttributeModifier On Sep 30, 2010 8:07 PM, Ichiro Furusato ichiro.furus...@gmail.com wrote: Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by a div element. The div element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on the div element that would replace everything within the div. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate the div element, then a modification of its 'class' attribute. Alternately, if I am required to create the div element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Programmatically change an HTML attribute value.
Hi Ichiro, This is what I use in a ListView to change the style attribute of table rows to give alternate colours to them. code item.add(new AttributeModifier(class, true, new AbstractReadOnlyModelString() { @Override public String getObject() { return (item.getIndex() % 2 == 1) ? even : odd; } })); /code I think you can add attributes like this to any component. On Fri, Oct 1, 2010 at 8:23 AM, James Carman ja...@carmanconsulting.comwrote: AttributeModifier On Sep 30, 2010 8:07 PM, Ichiro Furusato ichiro.furus...@gmail.com wrote: Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by a div element. The div element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on the div element that would replace everything within the div. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate the div element, then a modification of its 'class' attribute. Alternately, if I am required to create the div element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- Thanks Regards, James
Re: Programmatically change an HTML attribute value.
How about using a WebMarkupContainer? Then you can use an attribute modifier like the below example to set the class value. All you have to do is make the new component heirarchy match the markup heirarchy. div wicket:id=styler class=foo table form wicket:id=form ... /form /table /div In your panel you go from: add (new Form (form)); ... to: WebMarkupContainer c = new WebMarkupContainer (styler); c.add (new AttributeModifier (class, true, new ModelString(even))); add (c); c.add (new Form(form)); Another option might be to use a border to add the wrapping div and class attribute. Regards, Mike Hi Ichiro, This is what I use in a ListView to change the style attribute of table rows to give alternate colours to them. code item.add(new AttributeModifier(class, true, new AbstractReadOnlyModelString() { @Override public String getObject() { return (item.getIndex() % 2 == 1) ? even : odd; } })); /code I think you can add attributes like this to any component. On Fri, Oct 1, 2010 at 8:23 AM, James Carmanja...@carmanconsulting.comwrote: AttributeModifier On Sep 30, 2010 8:07 PM, Ichiro Furusatoichiro.furus...@gmail.com wrote: Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by adiv element. Thediv element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on thediv element that would replace everything within thediv. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate thediv element, then a modification of its 'class' attribute. Alternately, if I am required to create thediv element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Programmatically change an HTML attribute value.
Hmm. Thanks for the answers guys but from what I've understand so far from my own digging and your answers is that the container div I want to modify still needs to be created programmatically, and I'm distinctly trying to modify the attribute of one that begins its life in markup (i.e., in the .html file, not the .java file). It seems that may not be possible in Wicket so I'll probably have to push the contained markup into a fragment and add it to a programmatically-created element. Not quite what I wanted (and more complicated than I'd prefer) but doable. It's a shame something that can be accomplished with a simple DOM manipulation (get element by ID, then set its attribute value) ends up being fairly complicated. Thanks! Ichiro On 10/1/10, Michael O'Cleirigh michael.ocleir...@rivulet.ca wrote: How about using a WebMarkupContainer? Then you can use an attribute modifier like the below example to set the class value. All you have to do is make the new component heirarchy match the markup heirarchy. div wicket:id=styler class=foo table form wicket:id=form ... /form /table /div In your panel you go from: add (new Form (form)); ... to: WebMarkupContainer c = new WebMarkupContainer (styler); c.add (new AttributeModifier (class, true, new ModelString(even))); add (c); c.add (new Form(form)); Another option might be to use a border to add the wrapping div and class attribute. Regards, Mike Hi Ichiro, This is what I use in a ListView to change the style attribute of table rows to give alternate colours to them. code item.add(new AttributeModifier(class, true, new AbstractReadOnlyModelString() { @Override public String getObject() { return (item.getIndex() % 2 == 1) ? even : odd; } })); /code I think you can add attributes like this to any component. On Fri, Oct 1, 2010 at 8:23 AM, James Carmanja...@carmanconsulting.comwrote: AttributeModifier On Sep 30, 2010 8:07 PM, Ichiro Furusatoichiro.furus...@gmail.com wrote: Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by adiv element. Thediv element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on thediv element that would replace everything within thediv. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate thediv element, then a modification of its 'class' attribute. Alternately, if I am required to create thediv element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Programmatically change an HTML attribute value.
Ack, sorry, I managed to figure it out via Michael's suggestion. Here's the result. This show the relevant parts of embedding a userpanel into a templatepage: TemplatePage.html: div id=userdiv wicket:id=userdiv div id=user wicket:id=useruser/div /div!-- end user -- TemplatePage.java: WebMarkupContainer c = new WebMarkupContainer(userdiv); c.add(new AttributeModifier(class,true,new ModelString(even))); add(c); c.add(new UserPanel(user)); The resulting markup: div id=userdiv class=even div id=user!-- begin user . -- ... /div /div!-- end user -- and I'm able to programmatically alter the value of the 'class' attribute on the userdiv (wrapper div) element. It was a bit more complicated than I'd wished, but not onerously so, and frankly not much more code than using the DOM. Thanks very much guys! Ichiro On 10/1/10, Ichiro Furusato ichiro.furus...@gmail.com wrote: Hmm. Thanks for the answers guys but from what I've understand so far from my own digging and your answers is that the container div I want to modify still needs to be created programmatically, and I'm distinctly trying to modify the attribute of one that begins its life in markup (i.e., in the .html file, not the .java file). It seems that may not be possible in Wicket so I'll probably have to push the contained markup into a fragment and add it to a programmatically-created element. Not quite what I wanted (and more complicated than I'd prefer) but doable. It's a shame something that can be accomplished with a simple DOM manipulation (get element by ID, then set its attribute value) ends up being fairly complicated. Thanks! Ichiro On 10/1/10, Michael O'Cleirigh michael.ocleir...@rivulet.ca wrote: How about using a WebMarkupContainer? Then you can use an attribute modifier like the below example to set the class value. All you have to do is make the new component heirarchy match the markup heirarchy. div wicket:id=styler class=foo table form wicket:id=form ... /form /table /div In your panel you go from: add (new Form (form)); ... to: WebMarkupContainer c = new WebMarkupContainer (styler); c.add (new AttributeModifier (class, true, new ModelString(even))); add (c); c.add (new Form(form)); Another option might be to use a border to add the wrapping div and class attribute. Regards, Mike Hi Ichiro, This is what I use in a ListView to change the style attribute of table rows to give alternate colours to them. code item.add(new AttributeModifier(class, true, new AbstractReadOnlyModelString() { @Override public String getObject() { return (item.getIndex() % 2 == 1) ? even : odd; } })); /code I think you can add attributes like this to any component. On Fri, Oct 1, 2010 at 8:23 AM, James Carmanja...@carmanconsulting.comwrote: AttributeModifier On Sep 30, 2010 8:07 PM, Ichiro Furusatoichiro.furus...@gmail.com wrote: Hi, I'm assuming this is a dumb question but I wasn't able to locate an answer for what must be an extremely common need. I found the docs on How to modify an attribute on a HTML tag but that doesn't quite fit, as the examples are all for elements that are themselves created programmatically. Mine is solely in markup. I've got a lot of HTML markup surrounded by adiv element. Thediv element isn't created via Wicket, it's in my HTML file. It has a fair bit of descendent content so I don't want to have to create that via Java, as I'm assuming if I put a wicket:id on thediv element that would replace everything within thediv. I just want to programmatically alter its 'class' attribute, but I don't see how this is accomplished. E.g., div class=foo table ... etc. /table /div This would effectively be some kind of query to locate thediv element, then a modification of its 'class' attribute. Alternately, if I am required to create thediv element programmatically, how I attach a whole lot of HTML markup to it? Do I have to use a fragment? That seems like a lot of work to just alter an attribute value, so like I said, I must be missing something obvious here... Ichiro - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org