Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does <textarea> in React accept a value attribute but in HTML, it does not?

Not sure if this is React specific, but, why does the following work in React and render some text to the element:

<textarea value="Some text."></textarea>

but the same in plain HTML does not:

<textarea value="Some text."></textarea>

maybe I am missing something or done something foolish? Apologies and thanks in advance!

like image 609
xandert.93 Avatar asked Dec 06 '25 18:12

xandert.93


1 Answers

In HTML the text area is a non-self-closing tag and has content. It defines its text by children.

textarea

<textarea id="story" name="story" rows="5" cols="33">
  It was a dark and stormy night...
</textarea>

In React it uses the value attribute. Note that is also self-closing and takes no children.

textarea tag

This keeps the textarea element consistent with other form elements such as input and select.

<textarea
  id="story"
  name="story"
  rows="5"
  cols="33"
  value="It was a dark and stormy night..."
/>
like image 86
Drew Reese Avatar answered Dec 08 '25 06:12

Drew Reese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!