Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text width and height inside a TextArea in HTML using JavaScript?

Have this scenario:

Sample out put

Need to get the size of the red box inside the textarea. Basically dimension of the text, and not the dimension of the textarea.

the html code is just this:

<textarea style="width:450px;"></textarea>

Is there a way to achieve this, using Angular4+ or Javascript?

like image 335
Jacopo Sciampi Avatar asked Oct 19 '18 08:10

Jacopo Sciampi


People also ask

How do I change the width and height of a textarea in HTML?

The cols attribute specifies the visible width of a text area. Tip: The size of a text area can also be set by the CSS height and width properties.

How do you define the size of a textarea element?

The size of a text area is specified by the <cols> and <rows> attributes (or with CSS). The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the text area will be submitted). The id attribute is needed to associate the text area with a label.

Can we use value attribute in textarea?

From the MDN documentation: " <textarea> does not support the value attribute".

How to specify the size of a textarea using CSS?

Note: The size of a textarea can also be specified by the CSS height and width properties. Specifies the height of the text area (in lines). Default value is 2

How to get the text height of a text area?

Alternatively, you could not use textareas and just create a full css text box with p and divs. But, with textareas, this solution works perfectly, it grows and shrinks with input, even if the page loaded with pre-filled text (unlike most other solutions I was able to find). You can get the text height by getting the textarea scrollbar height

What is a text area in HTML?

Definition and Usage. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier). The size of a text area can be specified by the cols and rows attributes, or even better; through CSS' height and width properties.

How do I use textarea in form?

The <textarea> element is often used in a form, to collect user inputs like comments or reviews. A text area can hold an unlimited number of characters, and the text renders in a fixed-width font (usually Courier). The size of a text area is specified by the <cols> and <rows> attributes (or with CSS).


1 Answers

EDIT:

To calculate the width of an arbitrary string you could use this approach:

var fontSize = 12;
var widthTest = document.getElementById("widthTest");
widthTest.style.fontSize = fontSize;
var height = (widthTest.clientHeight + 1) + "px";
var width = (widthTest.clientWidth + 1) + "px"

console.log(height, width);
#widthTest
{
    position: absolute;
    visibility: hidden;
    height: auto;
    width: auto;
    white-space: nowrap; 
}
<div id="widthTest">
    FooBarEatsBarFoodBareFootWithBarFoo
</div>

If you want the width of a certain element the text is placed in using javascript you can do it like:

document.getElementById("myTextArea").offsetWidth

or

document.getElementById("myTextArea").clientWidth

INFO:

offsetWidth includes border width, clientWidth does not

In your case you need to apply an id to your textarea like:

 <textarea id="myTextArea" style="width:450px;"></textarea>

If you want to get the width in your angular component code:

someComponent.html:

 <textarea #myTextAreaRef style="width:450px;"></textarea>

someComponent.ts:

export class SystemComponent implements OnInit {

   @ViewChild('myTextAreaRef') public myTextAreaRef;
   ...
   ..

   someFunction() {
      console.log(this.myTextAreaRef.nativeElement.someAttribute)
   }
like image 106
iLuvLogix Avatar answered Sep 28 '22 03:09

iLuvLogix