Dave
Dave

Reputation: 19180

GWT: Trouble getting value from a text box

I'm using GWT 2.4. I'm trying to submit an AJAX request with the only input being the value of a text field on the page. Here is how I attach the handler to the page's button ...

public void onModuleLoad() {
    ...
    final com.google.gwt.dom.client.Element submitElement = Document.get().getElementById(SUBMIT_BUTTON_ID);
    final Button submitButton = Button.wrap(submitElement);
    ...
    // Add a handler to send the name to the server
    GetHtmlHandler handler = new GetHtmlHandler();
    submitButton.addClickHandler(handler);
}

But here's the problem. In my handler, whenever I try and get the value of the text field, it always returns the value entered in the text field when the page was first loaded, as opposed to what the most current value is ...

class GetHtmlHandler implements ClickHandler {
    /**
     * Fired when the user clicks on the submitButton.
     */
    public void onClick(ClickEvent event) {
        submitRequest();
    }

    /**
     * Send the name from the nameField to the server and wait for a
     * response.
     */
    private void submitRequest() {
        ...
        final Element nameFieldElement = DOM.getElementById(Productplus_gwt.NAME_FIELD_ID);

        // This always returns an old value.
        String docId = nameFieldElement.getAttribute("value");

Anyone know how I can write GWT code inside my handler to return the most current value of a text field given its page id?

Thanks, - Dave

Upvotes: 4

Views: 5153

Answers (2)

Gaurav Saxena
Gaurav Saxena

Reputation: 4297

Try using DOM.getPropertyString / DOM.getElementProperty

Following is the javadoc from GWT source for getAttribute function. It clearly says that the support for javascript's "getAttribute" function could be inconsistent for a few browsers and thus Element and subclasses should be used.

Alternatively you can use DOM.getPropertyString to fetch a value which uses object notation of javascript to get te current value

/**
* Retrieves an attribute value by name.  Attribute support can be
* inconsistent across various browsers.  Consider using the accessors in
* {@link Element} and its specific subclasses to retrieve attributes and
* properties.
* 
* @param name The name of the attribute to retrieve
* @return The Attr value as a string, or the empty string if that attribute
*         does not have a specified or default value
*/
public final String getAttribute(String name) {
    return DOMImpl.impl.getAttribute(this, name);
}

I tried using javascript's "getAttribute" function to get value of a text field in IE8 and FF6. IE gave the updated value of the text field while FF did not. Here is the fiddle

http://jsfiddle.net/GvNu4/

Upvotes: 2

Rohan
Rohan

Reputation: 7976

Well like you said it's an AJAX request so whatever code you have on ... the GWT code will continue to run.

You should use the callback of the request and check the value of the nameFieldElement at that moment.

Upvotes: 0

Related Questions