简体   繁体   中英

how to prevent html code reload on button click event in GWT

I created a button in GWT and on button click event i tried to get the text of div in alert box. so i did the following

Button btnClick = new Button("Click Me");
getElement().setInnerHTML("<div id='dummy'>This is dummy Text</div>");
btnClick.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
        // handle the click event
        Window.alert(getImageStr());
    }
});
RootPanel.get("dummy").add(btnClick);

And this is the method declaration:

private native String getDummyText() /*-{
        return document.getElementById("dummy").innerText;
}-*/;

But when i try to run it. it throws an exception

TypeError: Cannot read property 'innerText' of null

and when i saw in inspect element while stopping at break point in javascript, i was shocked to see many scripts in body tag and no HTML at that moment and when i release that break point, it created HTML again. Please anyone help me out. i am badly stuck in that. Thank You

You need to replace document with $doc in JSNI code:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM