user1032531
user1032531

Reputation: 26331

Use jQuery data() or attr() be used when value is originally written by the server?

Using PHP, I write some HTML to the page:

echo("<span id='myID' data-id='{$someValue}'>Hello</span>");

Then, later, I change the value of data-id using jQuery.

Then, even later, I use jQuery to get the value of data-id and use ajax to send to the server.

My question is should I use attr() or data()? Note my value is always a string or integer, and not an object. For instance....

$('#myID').attr('data-id',321);
var id=$('#myID').attr('data-id');

/or

$('#myID').data('id',321);
var id=$('#myID').data('id');

Upvotes: 0

Views: 52

Answers (1)

Snuffleupagus
Snuffleupagus

Reputation: 6755

Use data. No point in manipulating the DOM if you don't need to.

Upvotes: 1

Related Questions