Jamie Barker Jamie Barker - 10 months ago 34
jQuery Question

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

Which one is better:




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


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 :)