Many of us uses JQuery.attr() and JQuery.prop() and get the same result, as there is a small difference between the two.
Lets discuss it one by one.
JQuery.attr() :
It generates the value of an attribute for the first element in the set of matched elements.
JQuery.prop() :
It generates the value of a property for the first element in the set of matched elements.
Let make it more clear with an example:
<input type="text" id="name" value="findnerd" />
It has three attributes.
1. type
2. id
3. value
In this, attr() gives you the value of an element as it was defined in the HTML on page load.
But prop() always keeps the current state value.
That is why, It is always recommended to use prop() over attr() to get the values of elements as the element can be modified via JavaScript/jQuery in the browser at runtime.
0 Comment(s)