views:

667

answers:

3

With javascript,is it possible?

EDIT

let me be more specific.

I mean:the exact number of rows in a textarea.

Here it's 10 rows in my post.

Anyone knows?

A: 

You can access the field via Javascript DOM and simply count the number of newline characters.

oArea = document.getElementById('myTextField');
var aNewlines = oArea.value.split("\n");
var iNewlineCount = aNewlines.length();
webguydan
This will only work for <ENTER>. If the text auto-wraps the only way to do it is to use a fixed-size font, count all chars and then divide them by the allowed chars in a row. And that assuming there are no newlines in the process... \n anyone?!
Frankie
@Frankie,right,but how to calculate it correctly?
Mask
You could turn wrapping off if that is an option which would make the number of newlines = number of rows
John Scipione
@Mask look into thephpdeveloper answer. It's a more elegant approach than mine.
Frankie
@Frankie ,but I don't understand his approach,how is it supposed to calculate the rows?
Mask
+1  A: 

Well, cracking out from an autogrow function:

// usage
var tarea = document.getElementsByTagName('textarea')[0];
tarea.onkeyup = autogrow;
autogrow.call(tarea); // only include this if the textarea is going to have text in it from the beginning (i.e. before the user types anything in)

// the function
function autogrow() {
  if (!this.clone) {
    this.clone = document.createElement('textarea');
    this.clone.rows = this.rows;
    this.clone.style.overflow = 'auto';
    this.clone.style.visibility = 'hidden';
    this.style.width = this.clone.style.width = this.scrollWidth + 'px';
    this.clone.style.position = 'absolute';
    this.parentNode.insertBefore(this.clone, null);
    this.rows += 1;
  }
  this.clone.value = this.value;
  while (this.clone.scrollWidth < this.scrollWidth) {
    this.rows += 1;
    this.clone.rows += 1;
  }
}

Source: http://www.sitepoint.com/forums/showthread.php?t=642822

To get the row attribute:

var tarea = document.getElementsByTagName('textarea')[0];
tarea.onkeyup = autogrow;
autogrow.call(tarea);

// the function
function autogrow() {
  if (!this.clone) {
    this.clone = document.createElement('textarea');
    this.clone.rows = this.rows;
    this.clone.style.overflow = 'auto';
    this.clone.style.visibility = 'hidden';
    this.style.width = this.clone.style.width = this.scrollWidth + 'px';
    this.clone.style.position = 'absolute';
    this.parentNode.insertBefore(this.clone, null);
  }
  this.clone.value = this.value;
  var orirow = this.rows;
  while (this.clone.scrollWidth < this.scrollWidth) {
    this.rows += 1;
    this.clone.rows += 1;
  }
  // get the value from this.clone.rows
  this.rows = orirow; // set back the rows
}
thephpdeveloper
I don't understand.I need the total of rows,but you give me an autogrow function?
Mask
you can remove the auto grow and take the clone's row attribute, which is more accurate.
thephpdeveloper
I can't understand this piece of code now,can you edit so that it returns the result I need?
Mask
@Mask, read the comments on the last few line of the code at the 2nd code block.
thephpdeveloper
I tried to run it,but get "this.style is undefined..".Can you make it ready to go?
Mask
A: 

Well I found a much simplier way to do this, but you'll need to set the line-height of the textarea in the CSS. I tried to read the line height inside the script ta.style.lineHeight but it doesn't seem to return a value.

CSS

#ta { width: 300px; line-height: 20px; }

HTML

<textarea id="ta">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque suscipit, nisl eget dapibus condimentum, ipsum felis condimentum nisi, eget luctus est tortor vitae nunc. Nam ornare dictum augue, non bibendum sapien pulvinar ut. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras congue congue purus, quis imperdiet tellus ornare in. Nulla facilisi. Nulla elementum posuere odio ut ultricies. Nullam tempus tincidunt elit eget posuere. Pellentesque sit amet tellus sapien. Praesent sed iaculis turpis. Nam quis nibh diam, sed mattis orci. Nullam ornare adipiscing congue. In est orci, consectetur in feugiat non, consequat vitae dui. Mauris varius dui a dolor convallis iaculis.</textarea>

Script

 var taLineHeight = 20; // This should match the line-height in the CSS
 var taHeight = ta.scrollHeight; // Get the scroll height of the textarea
 ta.style.height = taHeight; // This line is optional, I included it so you can more easily count the lines in an expanded textarea
 var numberOfLines = Math.floor(taHeight/taLineHeight);
 alert( "there are " + numberOfLines + " lines in the text area");
fudgey