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
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.
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.
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.
This does the work for me in a textarea:
yourInputField.getElement().setPropertyString("placeholder", "enter your first name");
I think it is a HTML5 feature.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With