Naor
Naor

Reputation: 24103

what is better: data or attr?

What is better?

var itemId=8;
$('#aaa').data('ItemId', itemId);
$('#aaa').attr('ItemId', itemId);

data or attr?

Upvotes: 1

Views: 431

Answers (1)

Felix Kling
Felix Kling

Reputation: 817208

They serve different purposes.

  • If you want to store general data associated with an element, use .data().
  • If you want to change the attributes of the DOM element, use .attr().

Don't "invent" new attributes (appart from HTML5 data attributes). But as you are using jQuery, stick with .data().

Upvotes: 6

Related Questions