How do I get the event source child under mouse cursor?

492 views
Skip to first unread message

Marcel Karras

unread,
Mar 18, 2011, 6:41:59 AM3/18/11
to Google Web Toolkit
Hello,

imagine a <div> element that contains various other div child
elements. (huge amount) I'd like to get the child element of the
parent div when I hold down the mouse button. (MouseDownEvent) In
order to let the parent <div> be the relative container coordinate
calculations will be made on I registered a new MouseEventHandler to
that parent element. But now I have the problem of getting the deepest
DOM element under mouse cursor (resp. top visible element under mouse
cursor when event occures) The method event.getSource() will only give
me the parent div element and the only method to accomplish this seems
to be via JSNI as shown here: http://www.quirksmode.org/js/events_properties.html#target.
As I'd like to avoid JSNI where possible, is there a GWT driven way to
get the child element?

Greetings
- Marcel Karras

Jens

unread,
Mar 18, 2011, 7:00:40 AM3/18/11
to google-we...@googlegroups.com
I think you will get the deepest child (actually the real DOM target of the click event) via

public void onClick(final ClickEvent event) {

  Element e = DOM.eventGetTarget(Event.as(event.getNativeEvent()));

  System.out.println(e);

}



Thomas Broyer

unread,
Mar 18, 2011, 7:37:42 AM3/18/11
to google-we...@googlegroups.com
I'd rather use Element.as(event.getNativeEvent().getEventTarget())
or even replace the Element.as() with a check on Element.is() (just in case)

I always try to avoid the old DOM class, that predated the com.google.gwt.dom package.

Jens

unread,
Mar 18, 2011, 8:00:43 AM3/18/11
to google-we...@googlegroups.com
Oh is getEventTarget() new? Never recognized this method. 
So please use Thomas approach as it only depends on the event itself (and I will change my code now ;-) )

Thomas Broyer

unread,
Mar 18, 2011, 9:21:42 AM3/18/11
to google-we...@googlegroups.com


On Friday, March 18, 2011 1:00:43 PM UTC+1, Jens wrote:
Oh is getEventTarget() new?

(and NativeEvent didn't exist in 1.5)

Marcel Karras

unread,
Mar 18, 2011, 10:25:18 AM3/18/11
to Google Web Toolkit
Wow, you guys are really fast. Thanks a lot - works like a charm.
Reply all
Reply to author
Forward
0 new messages