I create a simple Javascript file containing a simple button.
function testfuncion() {
document.write('<input type="button" name="hello" value="hello">');
}
Then I created a HTML panel in gwt.
HTML panelHtmlTry = new HTML();
How can put the Javascript button into the HTML panel?
In this experiment I can't create the button in gwt but only put a Javascript object into the gwt panel.
PS: i can use html panel or horizzontal panel. My objective is put a javascript button on each GWT panel and show it
Yeah, there is a simpler way to do what you want. But if you want to do what you want. You can call the custom JavaScript method through JSNI.
This JSNI method calls your custom JS script, which is included in the host page.
$wnd is a reference to the browsers
window
object see GWT Docs on JSNIThen you can call this JSNI method anywhere in your GWT code:
And every time, the JavaScript function from the file gets called through JSNI.
You can temporarily redifine document.write, so that it sets the button in the
HTML
widget you wish.So you need to call testfunction, with the HTML widget, where the button should be positioned.
Let your testFunction return a string. This you can insert into your panelHtmlTry.
Given that no modifications are allowed to the JS file, and assuming the
input
is the onlyinput
in the document, you could extendHTML
to wrap theinput
element:It'll be best, of course, if you assign the
input
with anid
. That way you can look it up simply by callingRootPanel.get(id)
, thus avoiding the need for an index based search. This will robust your code, as changes in the DOM won't affect this lookup.