Notes on Using Textarea

Notes on Using Textarea
Why mention textarea specifically? Because the textarea node is actually very special. Because this node is special, it is interpreted differently in IE and other browsers.

I'll keep you in suspense for a while, haha! In fact, the main reason was that I helped a senior brother fix a BUG today. Specifically, it was because there was a failure when pulling data from the server and inserting it into the textarea. This situation only occurred in IE. Other browsers are all normal!

First, why is textarea special? In all form plug-ins, the value of textarea is written between two opening and closing tags, so its value can be regarded as a text node from the DOM perspective, which is unique to textarea. Because of this feature, you can still change the text of the textarea when you modify the value of its innerHTML. Although IE supports this, it does not allow one thing: dynamically inserting some html tags.

You might as well do an experiment:

When inserting a piece of HTML code through JS in IE, the innerHTML attribute is used instead of value or innerText.

I haven't tested IE9+, but IE6, 7, and 8 will all report errors.

It is probably for security reasons that JS is not allowed to dynamically insert HTML into the textarea, but careful people can find that if you type the HTML code manually, the textarea can accept it. I think this actually went through such a process: character escape. Yes, this is the only reason that can explain the success of manually entering the HTML code.

So, in my opinion, since so many browsers' textarea do not support HTML display, why do we still need to use the innerHTML attribute when performing JS operations? That is to say, no matter you use value to insert HTML code or innerHTML to insert HTML code, it will not be displayed after parsing (that's why there are rich text editors instead of textarea), so why not use value to set the value of textarea?

So I think that the value of textarea should not be set through innerHTML (of course it is even less likely to be innerText because of Firefox), but through value. This is something developers should pay attention to. Because some of the code I've seen recently always likes to use innerHTML. In fact, I think it all depends on the situation.

<<:  Complete step-by-step record of MySQL 8.0.26 installation and uninstallation

>>:  A great collection of web standards learning resources

Recommend

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

vue+el-upload realizes dynamic upload of multiple files

vue+el-upload multiple files dynamic upload, for ...

CSS realizes process navigation effect (three methods)

CSS realizes the process navigation effect. The s...

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

Vue.js framework implements shopping cart function

This article shares the specific code of Vue.js f...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

A brief discussion on CSS3 animation jamming solutions

Why is it stuck? There is a premise that must be ...

CSS to achieve zoom in and out close button (example code)

This effect is most common on our browser page. L...

Summary of common tool functions necessary for front-end development

1. Time formatting and other methods It is recomm...

Node.js+express+socket realizes online real-time multi-person chat room

This article shares the specific code of Node.js+...

Detailed tutorial for springcloud alibaba nacos linux configuration

First download the compressed package of nacos fr...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

Example code for implementing a pure CSS pop-up menu using transform

Preface When making a top menu, you will be requi...