Working with HTML Textarea Tag
HTML textarea tag allows us to put a big box where users can type in text or even some code. There are quite a few things we need to pay attention to when working with <textarea></textarea>.
- The size of textarea tag is not defined bt style, but by "rows" and "cols" attributes. Rows dictates the height while cols dictates the width, though, the textarea is drag-expandable.
- The value attribure is not renderd on the webpage, but its innerHTML is.
- When typing in the textarea, its value changes but not its innerHTML.
- The value of textareas is treated as string, when thers is a linebreak, there will be a "\n" hidden in the string.
Try Type in this textarea.
Value:
InnerHTML:
<textarea oninput="showvalue.innerHTML = this.value;showinnerHTML.innerHTML = this.innerHTML;" value="abc">123</textarea> <div>Value: <span id="showvalue"></span></div> <div>InnerHTML: <span id="showinnerHTML"></span></div>
What If I Inser a Line Break
- When you press "Enter" on the keyboard and inser a line break in the textarea. The value will have a hidden "\n".
- But When you render the string on the page, it will just be a space.
Try Type in "Enter"
<textarea oninput="showvalue_a.innerHTML = this.value.split('\n')" value=""></textarea>
<div id="showvalue_a"></div>
I use somestring.split("\n") to break the textarea value into an array and everytime when I hit "Enter", a new item is added to the array.
Syncing Value and InnerHTML for Textarea Tags
Another thing with textareas is that when displaying initail textarea values, we must put it between the textarea tags. And the it will become both the textarea's innerHTML and value.
When we call the textareas's value with innerHTML, it will return nothing.
<textarea id = "sometextarea">something to show</textarea> <script> console.log(sometextarea.value) //something to show console.log(sometextarea.innerHTML) //something to show </script>
on the other hand, the value property does not give the textareas any value at the onset.
<textarea id = "sometextarea" value = "something to show"></textarea> <script> console.log(sometextarea.value) //nothing console.log(sometextarea.innerHTML) //nothing </script>
Comments
Post a Comment