tags:

views:

63

answers:

1

I'm trying to figure out how to bind a javascript event to a select element in GWT, however the select element isn't being built in GWT, but comes from HTML that I'm scraping from another site (a report site from a different department). First, a bit more detail:

I'm using GWT and on load, I make an ajax call to get some HTML which includes, among other things, a report that I want to put on my page. I'm able to get the HTML and parse out the div that I'm interested in. That's easy to display on my page.

Here's where I get stuck: On the portion of the page I'm using, there's a select element which I can easily locate (it has an id), but would like to capture event if my user changes that value (I want to capture changes to the select box so I can make another ajax call to replace the report, binding to the select on that page, and starting the whole process again).

So, I'm not sure how, once I get the HTML from a remote site, how to bind an event handler to an input on that fragment, and then insert the fragment into my target div. Any advice or pointers would be greatly appreciated!

+1  A: 

How about this:

Element domSelect = DOM.getElementById("selectId");
ListBox listBox = new ListBox(domSelect);
listBox.addChangeHandler(new ChangeHandler() {
    void onChange(ChangeEvent event) {
        // Some stuff, like checking the selected element
        // via listBox.getSelectedIndex(), etc.
    }
});

Code untested, but you should get the general idea - wrap the <select> element in a ListBox. From there, it's just a matter of adding a ChangeHandler via the addChangeHandler method.

Igor Klimer
Instead of using "new ListBox(domSelect)", the trick is to call "ListBox.wrap(domSelect)". Works like a charm! Thanks Igor!
Todd R