Tuesday, June 10, 2008

How to get the tag name of a DOM element with GWT?

Surprisingly it is not possible to get the tag name value of a DOM element through the GWT API. For instance for a DOM element of a form, I would like to get the "FORM" string as a result.

Obviously it is possible to come with its own solution using the JSNI (java script native integration) and return the nodeName attribute of a DOM element, here is what I wrote:

public class Utils
* Returns an element tag name.
* @param element the element to obtain the tag name from
* @return the tag name value
public static native String getTagName(Element element)
// It should work with all browsers, if it does not, replace that with more appropriate code :-)
return element.nodeName;

There is probably a good reason explaining why it is not possible to get such a value, but for now it is unknown to me.

No comments: