Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a placeholder to a GWT text input field

Tags:

html

gwt

Does any know of a Google Web Toolkit text input widget that would display a message inside the field when it is empty?

E.g. a first name field that would say: 'enter your first name' and when the user starts typing the label is removed to show the typed text.

How would you go about this?

Daniel

like image 757
supercobra Avatar asked May 04 '12 14:05

supercobra


People also ask

How do I add a placeholder to a form?

Placeholders are commonplace in forms all over the web, including professional forms, so knowing how to put them in with HTML is important. So putting placeholders in forms in HTML is very simple. You just use the attribute, placeholder and set it equal in single or double quotes to the value that you want it to be.

What is placeholder in input tag?

The placeholder attribute specifies a short hint that describes the expected value of an input field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the input field before the user enters a value.

Where to use placeholder?

The placeholder attribute specifies a short hint that describes the expected value of a input field / textarea. The short hint is displayed in the field before the user enters a value.


2 Answers

This does the work for me in a textarea:

yourInputField.getElement().setPropertyString("placeholder", "enter your first name");

I think it is a HTML5 feature.

like image 199
Pablo Castilla Avatar answered Oct 05 '22 07:10

Pablo Castilla


With this solution you can define any additional properties in binder xml:

The solution:

public class MorePropertiesTextArea extends TextArea {
    private String moreProperties;

    public MorePropertiesTextArea() {}

    public void setMoreProperties(String moreProperties) {
        for (Entry<String, String> entry : parse(moreProperties).entrySet()) {
            getElement().setAttribute(entry.getKey(), entry.getValue());
        }
        this.moreProperties = moreProperties;
    }


    public String getMoreProperties() {
        return moreProperties;
    }

    private Map<String, String> parse(String moreProperties) {
        String[] pairStrings = moreProperties.split(";");
        HashMap<String, String> map = new HashMap<>();
        for (String pairString : pairStrings) {
            String[] pair = pairString.split("=");
            if(pair.length != 2)
                throw new IllegalArgumentException("at " + pair + " while parsing " + moreProperties + 
                        ". Use format like: 'spellcheck=false; placeholder=Write something here...'");
            map.put(pair[0].trim(), pair[1]);
        }
        return map;
    }

}

UI Binder xml:

<p2:MultiLineTextArea ui:field="taMultiLine" styleName="form-control muliLine"  
            moreProperties="spellcheck=false; placeholder=Write something here... " />

Result html:

<textarea class="form-control muliLine" spellcheck="false" placeholder="Write something here..."></textarea>
like image 42
Daniel Hári Avatar answered Oct 05 '22 06:10

Daniel Hári