find all checkboxes and set them to selected

910 views
Skip to first unread message

kwade

unread,
Aug 7, 2008, 10:42:16 AM8/7/08
to Google Web Toolkit
I've been using GWT for a few months now, mostly for XHR calls and
widgets. This is the first time I've attempted to use GWT to
manipulate existing html on a page.

My goal is simple: take an existing html page generated via struts
that has a variable number of checkboxes and turn them all to on/
selected. I know how to create a module/button with gwt, add a click
listener, etc. But, once my button is clicked to "select all
checkboxes"... what next?

How do I find the variable number of checkboxes on an existing html
page and set their values? Can someone point me in the right
direction?

I'm using GWT 1.4.

walden

unread,
Aug 7, 2008, 1:45:08 PM8/7/08
to Google Web Toolkit
Check out the DOM object in GWT, which has static methods for most of
what you want to do, including navingating from parent element to
child element, inspecting and setting element properties.
RootPanel.get().getElement() should be the body element of the
document (or something close). That should get you started. This may
be rather inefficient. It could be better if the checkboxes were also
GWT widgets.

Walden

meng

unread,
Aug 8, 2008, 12:57:08 AM8/8/08
to Google Web Toolkit
simple trick i always do b4 ajax is common..
HTML
<input type="checkbox" id="chkbox0" value="000"/>000<br />
<input type="checkbox" id="chkbox1" value="111"/>111<br />
<input type="checkbox" id="chkbox2" value="222"/>222<br />
<input type="checkbox" id="chkbox3" value="333"/>333<br />

GWT:
int i = 0;
while (true) {
final Element e = DOM.getElementById("chkbox" + i++);
if (e == null) {
break;
}
e.setPropertyBoolean("checked", true);

walden

unread,
Aug 8, 2008, 8:17:04 AM8/8/08
to Google Web Toolkit
meng,

Sure, but this implies that you have full authority over the content
of the page, as opposed to what I think is the case here where the GWT
developer gets to add stuff via javascript inclusion, after the page
has been authored. If the GWT developer has equal authority (author-
ity) on the page, then the strategy is different, because those boxes
are (at least could be) GWT widgets.

Walden
> > > I'm using GWT 1.4.- Hide quoted text -
>
> - Show quoted text -

abickford

unread,
Aug 8, 2008, 3:58:58 PM8/8/08
to Google Web Toolkit
i'm not so sure in 1.4. you may have to use JSNI. in 1.5 you can do
this:

Button b = new Button("Check all");
b.addSelectionListener(new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent ce) {
NodeList<Element> nl =
Document.get().getElementsByTagName("input");
for (int i = 0; i < nl.getLength(); i++) {
Element el = nl.getItem(i);
if (el.getTagName().toUpperCase().equals("INPUT")) {
if
(el.getPropertyString("type").toUpperCase().equals("CHECKBOX")) {
el.setPropertyBoolean("checked", true);
}
}
}
}
});
RootPanel.get().add(b);
return;
Reply all
Reply to author
Forward
0 new messages