简体   繁体   中英

Javascript - getting the exact value of a textarea

I need to get the exact value of a textarea field. There are many topics, but none of them meet my needs. Javascript transforms the html characters.

 console.log(document.getElementById('t1').value); // display: a'b => OK console.log(document.getElementById('t2').value); // display: a'b => KO I need to get a'b
 <textarea id="t1">a'b</textarea> <textarea id="t2">a&#039;b</textarea>

I must imperatively recover the exact content of the textarea (and not re-encode the content).

Do you have a solution for this issue?

Not JavaScript is transforming anything when reading the textarea's value, but your browser is rendering the entities as their corresponding characters. So, once the page is rendered, there's no entity inside the textarea anymore, it's just the text as the end user would see it. Therefore, neither .value nor .innerHTML will be able to retrieve the entity.

If what you're trying to achieve is displaying entities inside a textarea, you'll have to double-encode them as a&amp;#039;b for example, by using the entity &amp; for the ampersand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM