Textarea is a box with dynamic width and height, which means when text is entered in it, the text will not overflow and will only be contained within that texarea because It can dynamically increase or decrease its height and width. Textarea is mainly used inside forms to obtain the user's complete address and other content with large text content.
Typically, a text area is in the form of a square or rectangle, which can be changed using JavaScript and CSS. In this article, we will learn how to change the shape of a text area using JavaScript.
To change the shape of a text area using JavaScript, we can use different CSS properties with different values to shape the text area in different structures.
To change the shape of the text area to a parallelogram, we can use the style.transform property of CSS in JavaScript and use the value given by skew().
The following syntax will help you understand how to use the above method to change the shape of the text area -
selected_textarea.style.transform = "skew(40deg)";
Let's learn more about it by actually implementing it in a code example and see how it changes the shape of the text area.
Step 1 - In the first step, we will add a text area to the HTML document, and later we will change its shape using JavaScript.
Step 2 - In this step we will add a button element with an onclick event associated with it and later on when the button is clicked call a function and change the textarea's shape.
Step 3 - In the next step, we will define a JavaScript function in which we will grab the textarea element and change its shape to a parallelogram using the syntax written above . < /p>
Step 4 - In the last step, we assign a JavaScript function as a value to the button's onclick event so that it will be called later when the button is clicked.
The following example will explain to you how to use CSS's transform property to change the shape of a text area into a parallelogram -
<html> <body> <h2>Change the shape of a textarea</h2> <p id = "upper">The shape of below textarea will be changed once you click the button.</p> <textarea id = "textar">Enter something in the textarea.....</textarea> <br> <br> <button id = "btn" onclick = "changeShape()">Click to change the shape</button> <p id = "result"> </p> <script> var result = document.getElementById("result"); var upper = document.getElementById("upper"); function changeShape() { var textar = document.getElementById('textar'); textar.style.transform = "skew(50deg)"; upper.innerHTML = " The shape of the below textarea is changed as you clicked the button. "; result.innerHTML += " The shape of the textarea is changed to parallelogram using style.transform = 'skew(50deg)'"; } </script> </body> </html>
In the above example, we used the Transform property with the skew() value to change the shape of the text area to a parallelogram.
To change the shape of the text area to an oval, we can use the borderRadius property of CSS in JavaScript with a value of 50%.
You can follow the following syntax and use the borderRadius property to change the shape of the text area to an ellipse -
selected_textarea.style.borderRadius = "50%";
Let's look at the actual implementation of this method to understand how it works.
The algorithm of this example is almost similar to the algorithm of the previous example. You just need to make some small changes, replace the transform property in the above example with the borderRadius property, and set its value to 50% Get the text area oval shape.
The following example will illustrate how to use borderRadius to change the shape of a text area to an oval -
<html> <body> <h2>Change the shape of a textarea to an ellipse</h2> <p id = "upper">The shape of below textarea will be changed once you click the button.</p> <textarea id = "textar">Enter something in the textarea.....</textarea> <br> <br> <button id = "btn" onclick = "changeShape()">Click to change the shape</button> <p id = "result"> </p> <script> var result = document.getElementById("result"); var upper = document.getElementById("upper"); function changeShape() { var textar = document.getElementById('textar'); textar.style.borderRadius = "50%"; upper.innerHTML = " The shape of the below textarea is changed as you clicked the button. "; result.innerHTML += " The shape of the textarea is changed to ellipse using style.borderRadius = '50%'"; } </script> </body> </html>
In the above example, we use JavaScript to change the shape of the text area from a rectangle to an ellipse with the help of the borderRadius property.
In this article, we discuss in detail two different methods of changing the shape of a text area into two different shapes, with the help of code examples for each method. It is also possible to change the shape of a textarea to another shape using other CSS properties in JavaScript, so keep searching and keep learning.
The above is the detailed content of How to change the shape of a text area using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!