Reputation: 26331
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
Reputation: 6755
Use data
. No point in manipulating the DOM if you don't need to.
Upvotes: 1