I'm just wondering what the difference is between the two. I have noticed the two methods give different results at times.
The getAttribute() method of the Element interface returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string); see Non-existing attributes for details.
Value - The Element of Shadow Value deals with the lightness or darkness of a color. Since we see objects and understand objects because of how dark or light they are, value is incredible important to art.
The getAttribute() method is declared in the WebElement interface, and it returns the value of the web element's attribute as a string. For attributes having boolean values, the getAttribute() method will return either true or null.
JavaScript getAttribute() method. The getAttribute() method is used to get the value of an attribute of the particular element. If the attribute exists, it returns the string representing the value of the corresponding attribute. If the corresponding attribute does not exist, it will return an empty string or null.
The difference is that element.value
is real time and if a user changes let's say, a textbox input, it will reflect that, and show you the new value.
While getAttribute('value')
will still show the original value="whateverWasHere"
value.
jsFiddle DEMO
.value
does not map to any attribute.
.defaultValue
maps to the "value"
attribute. So when you say elem.getAttribute("value")
that's the same as elem.defaultValue
.
Additionally, .defaultValue
reflects .value
when the input is untouched (dirty value flag is false). After the input's value is changed by user interaction, this mapping stops. While the input is untouched, you can change .defaultValue
(and thus .setAttribute("value")
) and see it change .value
as well. Not that this is practically useful but interesting piece of trivia nevertheless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With