Jamie Barker Jamie Barker - 3 months ago 9
jQuery Question

Which jQuery method is better: ".attr('data-thing');" or ".data('thing');"

Which one is better:

$('div').attr('data-stuff');


or

$('div').data('stuff');


After having a discussion in the comments on this thread, I'm interested to know which is better to use, if any.

Answer

Taking what all people have said into consideration, here is "the answer":

.data is better because:

  1. it's slightly shorter to type.
  2. It can protect you from possible future changes to data structure (although unlikely).

.attr is better because:

  1. it loads nearly 3 times faster.

I think I will continue using .attr myself, but thank you all for this information :)