0

I am trying to add data-id attribute in the table column. So, I can add like these two ways.

$(this).data('id',10);

And,

$(this).attr('data-id',10);

What is the exact difference of these two methods. And which one is better to use in jQuery.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • `data()` reads from jQuery's in-memory cache (and is therefore faster once that object has been created) while `attr()` reads from the DOM. See the duplicate for more information – Rory McCrossan Feb 28 '19 at 08:38
  • read here https://stackoverflow.com/questions/7261619/jquery-data-vs-attr – Afif Zafri Feb 28 '19 at 08:39
  • 1
    Probably in the dup:`.data()` will apply type coercion while `.attr()` will always be a string. `
    ` `$(this).data('id') === 10` `$(this).attr('data-id') === "10";`
    – freedomn-m Feb 28 '19 at 09:08

0 Answers0