GWT
offers two ways of retrieving an HTML element by its unique ID
.
What's the diference (if there is one) between :
Gets the element associated with the given unique id within the entire document.
@param id the id whose associated element is to be retrieved
@return the associated element, or null if none is found
Returns the Element whose id is given by elementId. If no such element exists, returns null. Behavior is not defined if more than one element has this id.
@param elementId the unique id value for an element
@return the matching element
JavaScript getElementById () Method: This method returns the element that has the ID attribute with the specified value. It is the most used HTML DOM method to manipulate, or get info from, an element on your document.
In the example above, getElementById is a method, while innerHTML is a property. The most common way to access an HTML element is to use the id of the element. In the example above the getElementById method used id="demo" to find the element.
HTML DOM methods are actions you can perform (on HTML Elements). HTML DOM properties are values (of HTML Elements) that you can set or change. The HTML DOM can be accessed with JavaScript (and with other programming languages). In the DOM, all HTML elements are defined as objects.
document.getElementById (‘demo’) means a reference to the element on your document which has the ID of demo. Seemingly, this doesn’t help in understanding.
Basically nothing. At some point during GWT's lifecycle the whole DOM related code was rewritten into the dom
package. In the new package for each HTML tag a specific Element class is available, like DivElement
to provide specific methods for these tags. For example in you example if you would use it to look up div elements you could directly use the DivElement
. The code would for both versions look as follows:
DivElement divID = (DivElement) Document.get().getElementById("divID");
or
DivElement divID = (DivElement) DOM.getElementById("divID").cast();
To be backward compatible the old code was kept. All Widget classes use the old Element
class which is also returned by DOM.getElementById
. The old Element
class was changed and extends the new Element
class, without any extra's. So they are basically the same. In general you should just use the Document.get()
. This all can make it somewhat confusing when working with elements.
Look:
in com.google.gwt.dom.client.Document
public final native Element getElementById(String elementId) /*-{
return this.getElementById(elementId);
}-*/;
in com.google.gwt.user.client.DOM
public static Element getElementById(String id) {
return Document.get().getElementById(id).cast();
}
So it's just a "convenience wrapper method".
But although they both return Element
these ara Element
s from different packages, and as both Element
s are JavaScriptObject
and mean the same you can cast between them ignoring inheritance hierarchy either with java cast syntax ()
or with convenience method JavaScriptObject.<T extends JavaScriptObject> cast()
Btw. Document is JSO overlay type so "native this
" points at this
;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With