Description:
I am using simple javascript
to set a the value of an input
. I am using multiple methods that appear to be the same but with different results. Here is an example:
HTML:
<input name="testinput" value="" type="text" />
Javascript:
var input = document.getElementByTagName('input')[0];
input.value = "5"
console.log(input.value) // returns "5"
console.log(input.getAttribute("value")) // returns ""
Of course the functionality is reversed when using the setAttribute()
function. Yet, when on form submit they both give a input=5
result.
Question:
What is the point of separating the two properties? is the .value
stored differently than the .getAttribute("value")
?
Disclaimer:
I have read:
- When to use setAttribute vs .attribute= in JavaScript?
- Setting a property via property or setAttribute
Both of those question/answers left me confused and unsatisfied.