14
I notice that there is a difference between updating or applying an attribute data
using the jQuery method .attr()
and the method .data()
.
The examples below can be seen in this Jsfiddle.
Apply value to non-existent attribute
If the value is applied by the method
.attr()
and then change the same by the method.data()
, the result of collecting the value varies according to the method used to apply it:<div id="myBanana"></div>
Apply
// utilizando .attr() $( "#myBanana" ).attr( "data-fail", "no banana" ); // utilizando .data() $( "#myBanana" ).data( "fail", "no monkey" );
Collect
// utilizando .attr() da "no banana" console.log( $( "#myBanana" ).attr( "data-fail" ) ); // utilizando .data() dá "no monkey" console.log ( $( "#myBanana" ).data( "fail" ) );
Apply value to existing attribute
If the attribute already exists in the widget, when collecting it with any of the methods, I receive the existing value.
After a change to the value, depending on the method used, the value applied is only correctly collected if you use the same method to obtain it:
<div id="myMonkey" data-fail="banana"></div>
Collect
// utilizando .attr() dá "banana" console.log( $( "#myMonkey" ).attr( "data-fail" ) ); // utilizando .data() dá "banana" console.log ( $( "#myMonkey" ).data( "fail" ) );
Alter
// utilizando .attr() $( "#myMonkey" ).attr( "data-fail", "No banana" ); // utilizando .data() $( "#myMonkey" ).data( "fail", "More banana" );
Reapply
// utilizando .attr() dá "No banana" console.log( $( "#myMonkey" ).attr( "data-fail" ) ); // utilizando .data() dá "More banana" console.log ( $( "#myMonkey" ).data( "fail" ) );
At first both methods were to collect the same value that was already present in the attribute
data
, but after changing this value, I have to use the same method to apply and collect values or the results diverge.
It is obvious that the attribute is called data-*
, is not directly related to the method .data()
but with the method .attr()
.
In addition both methods seem to interact with the attribute data
up to a certain point, but each has its own way of storing new assigned values.
When analyzing what happens in terms of DOM manipulation, there are also noticeable differences substances caused by the use of each of the methods:
The method .attr()
is actually updating the value of the attribute data
while the method .data()
does not change the value of the attribute, but attaches an identifier to the element with its value.
Question
What is the difference between the method .attr()
and the method .data()
in terms of use, purpose and impact on the element, to make use of arbitrary data assignment that HTML5 makes available ?
Interesting, in short the method
.attr()
gives us the present value in the.data()
gives us the value of the Markup when there is no assignment after loading the page, or the current value if already changed! The.prop()
since version 1.9 of jQuery assumes the same behavior as the method.data()
, got it right?– Zuul
@Zuul actually I’m confused, after I posted the answer I did some more tests and I noticed that the
data
completely changed its behavior (as the bfavaretto said, this was after HTML5 consolidated the attributesdata-*
). I’m still not quite sure how he behaves. But anyway, the idea I wanted to pass on in this answer was that theattr
refers to the text of the Markup, and theprop
refers to a property (field, member) of the object in the FOD.– mgibsonbr
@Zuul (clarifying: the behavior was one last time I checked - around the version
1.6
- and today is another; I had not attempted to that fact when I formulated the answer)– mgibsonbr
Excellent explanation of
attr
. Detail: if you set/change the value of an attribute withattr
, and then try to read it again withattr
, the value read will be the new one. Therefore, theattr
not only reads the initial value, it simply combinesgetAttribute
andsetAttribute
.– bfavaretto
@bfavaretto Correct! But the important thing is that the property is not affected by this change. Ex.: type "test" in the text box; use the code
$("input").attr("value", "foobar")
; after this code, if you look at the text box you will see that its value is still "test". If you use aform submit
without Javascript, the value passed will still be "test". That is, even if there is correspondence between theattr
of jQuery and thegetAttribute
andsetAttribute
of the GIFT, there is no correspondence between them and the property itself.– mgibsonbr
Interesting, it seems to be a special case of Forms. I tested with
rel
andhref
(which I believe are properties), and changing the attribute also changed the property (http://jsfiddle.net/LD3GX/).– bfavaretto