Have this scenario:
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?
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.
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.
From the MDN documentation: " <textarea> does not support the value attribute".
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
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
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.
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).
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)
}
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