LancerX
LancerX

Reputation: 1231

Set value to GWT checkbox

I want to set value (number or string) to checkbox. This code

final CheckBox checkBox = new CheckBox("Some label");
checkBox.getElement().setAttribute("value", i.toString());
checkBox.getElement().getStyle().setProperty("color", colorList.get(i));
checkBox.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            Object sender = event.getSource();
            if (sender == checkBox) {
                CheckBox checkBox = (CheckBox)event.getSource();
                Window.alert(checkBox.getFormValue());
            }
        }
});

creates the following HTML:

<td align="left" style="vertical-align: top;">
<span class="gwt-CheckBox" value="3" style="color: rgb(128, 105, 155);">
<input id="gwt-uid-4" type="checkbox" value="on" tabindex="0">
<label for="gwt-uid-4">Some label</label>
</span>
</td>

The attribute value is set to span instead of input. The Window.alert(checkBox.getFormValue()) shows message with string 'on', instead of '3'.

Upvotes: 1

Views: 3008

Answers (2)

Suresh Atta
Suresh Atta

Reputation: 122018

Set the value for checkBox to "someValue" using setFormValue

  checkBox.setFormValue("someValue");

Upvotes: 2

Adarsha
Adarsha

Reputation: 895

The property "value= on / off" has some predefined meaning for the check box. So don't use the property "value" to store numbers.

If you really want to store the numbers use your own custom properties like -

checkBox.getElement().getFirstChildElement().setAttribute( "customProperty", "3" );

and to access the property use -

checkBox.getElement().getFirstChildElement().getAttribute( "customProperty" );

Upvotes: 3

Related Questions