John Manak
John Manak

Reputation: 13558

Wicket - Radio Button and Label element

In my Wicket app, I have a page with radio buttons and <label for="..."> elements:

<input type="radio" wicket:id="today" id="today" />
<label for="today">Today</label>

However, while the radio button's id property gets changed automatically, the for property of the label tag stays the same and that creates an inconsistency (the label isn't linked to the button anymore). What's the best way to address this? Right now, I address it this way:

add(
    new Label("todayLabel", "Today")
        .add(new AttributeModifier(
             "for",
             new Model<String>(today.getMarkupId()
 )));

but that's not very nice. Is there another, clearer way to link these to tags?

Upvotes: 3

Views: 3643

Answers (2)

jpo
jpo

Reputation: 2660

You can do it all in the markup:

<label wicket:for="today">
   <input type="radio" wicket:id="today" id="today" />
   Today
</label>

More about wicket's html tags here: https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html

Upvotes: 4

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299048

Use FormComponentLabel:

add(new FormComponentLabel("todayLabel", today));

Upvotes: 4

Related Questions