The data-*
attributes & jQuery's .data()
method are not interchangeable. Use this to get it:
t.action = $(this).attr('data-action');
You seem to be mixing jQuery's data
method with HTML5's data-*
attributes. These are two different things. The only time they interact with one another is when you first call .data
on an element.
When you make a call to .data
, jQuery looks for any data-*
attributes, and adds it to the data
collection. However, this only happens once. Subsequent calls to .data
will not look at the element's data-*
attributes.
To quote the jQuery docs:
The data-
attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).
Since you're using .attr('data-action', 'follow')
to set it, you should use .attr('data-action')
to get it.
Note: $('.action[data-action=unfollow]')
will not select an element that had the action
set via jQuery's .data
. Again, the two are not interchangeable.