Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:51:36+00:00 2026-06-10T21:51:36+00:00

Possible Duplicate: jQuery .data() Not Updating DOM I am having a problem with using

  • 0

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:

  1. load page, the data-action=’unfollow’
  2. click this, api call gets made and you are not following this user; the callback sets data-action=’follow’
  3. click this value again and the data-action is echoed as ‘unfollow’ rather than ‘follow’

How could I tell jQuery to refresh the bindings of this event? I thought this was what $(document).on does.

thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:51:38+00:00Added an answer on June 10, 2026 at 9:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
Possible Duplicate: jQuery .load method not firing on IE9 Hi I'm using a simple
Possible Duplicate: jQuery: Return data after ajax call success I have the following function
Possible Duplicate: jquery autocomplete this.source is not a function error I have an autocomplete
Possible Duplicate: Check that element is in DOM or not with jQuery? So I
Possible Duplicate: Detect all changes to a <input type=text> (immediately) using JQuery I have
Possible Duplicate: AJAX- response data not saved to global scope? I basically have a
Possible Duplicate: Access Control Allow Origin not allowed by I have a problem when
Possible Duplicate: jQuery: how do I animate a div rotation? I have a div
Possible Duplicate: jQuery won't parse my JSON from AJAX query So I have parsed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.