How to add handler at lower level

35 views
Skip to first unread message

saurabh saurabh

unread,
Dec 9, 2011, 9:32:44 AM12/9/11
to Google Web Toolkit
Hi everyone,
I need to know how to add handler to a node or
element in Html document. Suppose I am not using any widget or panel
or any class of gwt. and I have something like this

<div id="i_need_clickHandler"></div>

//.........

Element ele = doc.getElementById("i_need_clickHandler");

So how can I attach a click or mouseover handler to ele.

Thanks in advance

Jens

unread,
Dec 9, 2011, 10:01:08 AM12/9/11
to google-we...@googlegroups.com
I have used

com.google.gwt.dom.client.Element e = Document.get().getElementById(...).cast();
DOM.setEventListener(e, new EventListener() {
  public void onBrowserEvent(Event e) {
    NativeEvent ne = e.cast();
    if("click".equals(ne.getType())) {
       //click event occured
    }
  }
});

But maybe there is a better way of doing this.

-- J.

Thomas Broyer

unread,
Dec 9, 2011, 10:15:54 AM12/9/11
to google-we...@googlegroups.com
You have to make sure you call DOM.setEventListener(e, null); at some point, or you'll have memory leaks in IE. See http://code.google.com/p/google-web-toolkit/wiki/DomEventsAndMemoryLeaks

An alternative would be to use GwtQuery (which uses setEventListener under the hood, AFAICT): http://code.google.com/p/gwtquery/
Reply all
Reply to author
Forward
0 new messages