I want to call from GWT a javascript function that accepts element id of element created by UiBinder. Functions fails because it cannot find that element. After some debugging I found this strange behaviour:
log.info(DOM.getElementById("MyId").getAttribute("id"));
ScriptInjector.fromString("console.log(document.getElementById(\"MyId\"))").inject();
checkMyId();
log.info(DOM.getElementById("MyId").getAttribute("id"));
Results in:
MyId
null
null
MyId
checkId
is defined as:
public static native void checkMyId()/*-{
console.log(document.getElementById("MyId"));
}-*/;
Both ScriptInjector
and native functions seems to have limited access to elements compared to DOM#getElementById
. What is reason for this behaviour? How can I call javascript function so it can access that element?
I've tried using gwt 2.8.0-beta1 and 2.8.0-rc1 with the same results.
When accessing the browser’s window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page’s window and document.