Jamie Barker Jamie Barker - 1 year ago 55
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.

Answer Source

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