Let’s have, say, an element with the text content of Clark, Theodore S.
. Note the double white spaces after the "Theodore". Say you have this element be an HTML option, like this: <option>Clark, Theodore S.</option>
. If you try to extract its text content, you would correctly get the result Clark, Theodore S.
. However, if you try to get the option’s value
property, you get Clark, Theodore S.
.
As per MDN’s docs, white spaces are often times ignored when formatting the DOM. However, wouldn’t it be logical to have the textContent
property return the trimmed value and the value
property return the exact value? As far as I know, when you get the value
of an input element no trimming is performed, so why is it done here?
Why is the value
property modified in any way when it is expected that it would most accurately reflect the actual value set? Is this a bug in JavaScript or is it explicitly intended?
2
Answers
The
textContent
of a HTMLOptionElement is not its value per se. Its value is/should be defined by its value attribute. If no value attribute is present, the text content is taken and apparently that content is cleaned up.from the MDN link above:
If you need the exact value without a value attribute,
innerText
ortextContent
orinnerHTML
may be the alternative. Here’s a comparison:It’s because in the absence of a
value
attribute, the.text
value is used as the<option>
‘s value.From the specs for
HTMLOptionElement.prototype.text
Thanks to this, we can write stuff like the following and still have the value
foo bar
being sent to the server as is likely expected.