Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Element.value and Element.getAttribute("value")

I'm just wondering what the difference is between the two. I have noticed the two methods give different results at times.

like image 783
ama2 Avatar asked Aug 15 '12 16:08

ama2


People also ask

What is getAttribute value?

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.

What does element value do?

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.

What is the return type of getAttribute ()?

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.

What should the getAttribute () method return if the attribute value is false?

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.


2 Answers

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

like image 148
Mark Pieszak - Trilon.io Avatar answered Oct 21 '22 01:10

Mark Pieszak - Trilon.io


.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.

like image 41
Esailija Avatar answered Oct 21 '22 02:10

Esailija