finding elements via css selector

779 views
Skip to first unread message

Jason Essington

unread,
Sep 29, 2006, 11:07:35 AM9/29/06
to Google-We...@googlegroups.com
I just noticed this note in the HTML page generated by GWT:

// Assume that the host HTML has elements defined whose
// IDs are "slot1", "slot2". In a real app, you probably
would not want
// to hard-code IDs. Instead, you could, for example, search
for all
// elements with a particular CSS class and replace them with
widgets.
//

The part I thought was interesting was searching for elements with a
particular CSS class.

I don't really see any easy way to do that with GWT. Did I miss
something?

I know that prototype allows elements to be found using a css
selector rather than an ID, but I didn't see any similar
functionality in GWT.

For integrating GWT into existing HTML pages it might be handy to
have something like

RootPanel.getElementsByCSS(String selector)
or perhaps more appropriately a DOM method for that
DOM.getElementsByCSS(Element element, String selector)
then you could for instance find all elements of class "TD.my-gwt-
widget" by doing.

List element = DOM.getElementsByCSS(RootPanel.getBodyElement(),
"TD.my-gwt-widget");

If this is something that others would find useful I would be happy
to file a feature request.

-jason

Sandy McArthur

unread,
Sep 29, 2006, 11:16:21 AM9/29/06
to Google Web Toolkit
I've used getElementsByCSS utils before when writing unobtrusive
JavaScript in the past. It's a nice way to decorate a page with extra
functionality with only adding a script tag to the source. In my
somewhat-limited experience with GWT I've never had a need to find an
element because when I create a new widget I immeaditly have a handle
to manipulate it with. Personally, I don't think it would hurt GWT but
I don't think it would get much use either.

IamRobe...@gmail.com

unread,
Sep 29, 2006, 1:36:26 PM9/29/06
to Google Web Toolkit
The GWT-WL has this. It could probably use some tweaking, but has done
the job for me.

List elements = SearchUtils.findElementsForClass("className");

http://gwt-widget.sourceforge.net/docs/xref/index.html

Having the ability to use any valid CSS selector would be extremely
useful I would think.

Rob

IamRobe...@gmail.com

unread,
Sep 29, 2006, 1:39:55 PM9/29/06
to Google Web Toolkit
Sorry, incorrect link in the last post.

[source]
http://gwt-widget.sourceforge.net/docs/xref/org/gwtwidgets/client/util/SearchUtils.html

[javadoc]
http://gwt-widget.sourceforge.net/docs/apidocs/org/gwtwidgets/client/util/SearchUtils.html

...And I had the incorrect signature.

List elements = SearchUtils.findElementsForClass(widget.getElement(),
"className");

Rob

Reply all
Reply to author
Forward
0 new messages