0

Possible Duplicate:
How to get number of rows in <textarea >?

I have a div inside a page layout, and the user will enter text in it:

 <div> user text will go here </div>

I would like to count the number of text rows the div actually occupies in the layout once the user has entered text. Notice this will vary according to the screen resolution, or the window size.

Is there a way to do so?

Even better, is there a way to get the div height in pixels?

I'm using GWT, but a JavaScript solution would do as well.

Thanks!

Community
  • 1
  • 1
Mike
  • 1,296
  • 2
  • 15
  • 40
  • 1
    Javascript answer: http://stackoverflow.com/questions/1760629/how-to-get-number-of-rows-in-textarea – JSW189 Oct 25 '12 at 15:09

1 Answers1

3

Even better, is there a way to get the div height in pixels?

If you have a com.google.gwt.user.client.Element, then just call

element.getClientHeight()

From the Javadoc of getClientHeight():

Returns the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin.

If you have a Widget, e.g. a com.google.gwt.user.client.ui.Label, use

label.getElement().getClientHeight()

In some circumstances, it may be useful to call this within Scheduler.scheduleDeferred, to make sure all styling has been applied before the height calculation is performed.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • 2
    Once you have a div height, as Chris suggested, you can divide it by line-height property to get the number of rows if you need it. – Andrei Volgin Oct 25 '12 at 16:58
  • Also, you can call getOffsetHeight() directly on a widget, without getting its element. – Andrei Volgin Oct 25 '12 at 16:59
  • 1
    @Andrei: Good point. But note, that `getOffsetHeight()` is available on Widget, whereas `getClientHeight()` (which would be the correct one in this situation) is not. As long as there's no padding/border on the div, it's the same value. – Chris Lercher Oct 25 '12 at 17:12