How to get text from textarea as html

266 views
Skip to first unread message

Deepak Singh

unread,
Mar 29, 2012, 4:33:10 PM3/29/12
to google-we...@googlegroups.com
Hi,

I have a Textarea and i want to get the text written from this in the same same format as user has entered.
Means, with spaces and line breaks same as user has entered.
How can i achieve this ?

Currently it gives the text without line breaks with textarea.getText().

Thanks
Deepak Singh

dodo dard

unread,
Mar 30, 2012, 3:25:52 AM3/30/12
to google-we...@googlegroups.com
Where do you want to write ? If is in the HTML directly you must to replace the new line character and the space character with appropriate html code. 

Check this out :


Hope this help,

Bowie
==============

Joseph Lust

unread,
Mar 30, 2012, 12:25:15 PM3/30/12
to google-we...@googlegroups.com
Deepak,

Is there a reason you're not using a TextAreaElement? That has the getInnerHTML() method that will return the raw values from the TextArea.

Sincerely,
Joseph

Deepak Singh

unread,
Mar 30, 2012, 5:18:49 PM3/30/12
to google-we...@googlegroups.com
I have a ui:binder,

there i use <g:Textarea ui:field ="ta"/>

Now, when i do - ta.getText() -> returns all textarea data without spaces and line breaks.

If i do - ta.getElement().getinnerHTML() -> returns blank. No data at all.

Thanks
Deepak


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/8w0_YcK8_JQJ.

To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.



--
Deepak Singh

Jens

unread,
Mar 30, 2012, 7:09:56 PM3/30/12
to google-we...@googlegroups.com
TextArea.getText() gives you exactly the text with all white spaces and line breaks.

Its just that you can't put that text into a HTML element, because the browser will remove every line break and also trims down the white spaces to one.

If you want to display that text, you have to use the <pre> tag or apply the CSS property "white-space:pre" to any HTML element that should contain the text from the textarea.

Example:

  @Override
  public void onModuleLoad() {
    final Label label = new Label();
    final Label preLabel = new Label();
    preLabel.getElement().getStyle().setProperty("whiteSpace", "pre");
    final PreElement pre = Document.get().createPreElement();
    final TextArea textarea = new TextArea();
    textarea.setSize("300px", "200px");
    Button button = new Button("Read text");
    button.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(final ClickEvent event) {
        String text = textarea.getText();
        System.out.println(text);
        label.setText(text);
        preLabel.setText(text);
        pre.setInnerText(text);
      }
    });
    RootPanel.get().add(textarea);
    RootPanel.get().add(button);
    RootPanel.get().add(new Label("------ LABEL -------"));
    RootPanel.get().add(label);
    RootPanel.get().add(new Label("------ PRE ELEMENT -------"));
    RootPanel.get().getElement().appendChild(pre);
    RootPanel.get().add(new Label("------ LABEL (with CSS white-space:pre applied) -------"));
    RootPanel.get().add(preLabel);
  }

Reply all
Reply to author
Forward
0 new messages