Possible Duplicate:
jQuery .data() Not Updating DOM
I am having a problem with using the on attribute. I have written a small set of methods to send api calls.
The markup is like this:
<div data-global-id="1318" data-action="unfollow" class="action text-as-link follow-btn btn" style="text-decoration: none;">unfollow</div>
and have an event capture like this:
$(document).on('click','.action', function(){
var t={};
t.args={};
t.args.global_id=$(this).data('global-id');
t.global_id=t.args.global_id;
t.action=$(this).data('action');
t.identifier=t.action + '_v2';
alert('here is action: ' + t.action);
api_post_v1(t);
});
The api_post_v1 correctly sends the call.
There is some code to handle the callback and it sets the markup to:
<div data-global-id="1318" data-action="follow" class="action text-as-link follow-btn btn" style="text-decoration: none;">follow</div>
The code for this is like:
$foo=$('.action[data-action=unfollow][data-global-id='+global_id+']');
$foo.attr('data-action','follow');
The key piece is the data-action. I'd like the call to the event handler above to say it is 'follow' but it says it is still 'unfollow'.
The sequence is the following:
How could I tell jQuery to refresh the bindings of this event? I thought this was what $(document).on does.
thx
If you are checking it and it doesn't already have one, it creates the value based on the data attribute that is in the DOM. . attr() is for setting or checking the DOM element's attribute value and will not touch the jQuery data value. If you need them both to change you should use both . data() and .
To retrieve a data-* attribute value as an unconverted string, use the attr() method. Since jQuery 1.6, dashes in data-* attribute names have been processed in alignment with the HTML dataset API. $( "div" ).
Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document.
You can use the attribute selector: $('[data-my-key]').
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With