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

  • Home
  • SEARCH
  • 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 8031753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:12:15+00:00 2026-06-05T01:12:15+00:00

I have a problem with one jQuery script. I use this code: <span class=follow

  • 0

I have a problem with one jQuery script. I use this code:

<span class="follow" data-id="<?=$m['id']; ?>" ></span>

–

        $('.follow').click(function(){

        $.ajax({
            type: "POST",
            url:"../ajax/settings/follow.php", 
            async: false,
            dataType:'html',
            data: {'id':$(this).attr('data-id')},
            success:function(rs){
               $(this).removeClass("follow"); 
                $(this).addClass("unfollow"); 

            },

            error:function (xhr, ajaxOptions, thrownError){
                alert("ERROR !!!");
                alert(xhr.status);
                alert(ajaxOptions);
                alert(thrownError);

            }  
        });   

    });

After click the span with style image TICK changed to CROSS, and changed attr class to unfollow. After click unfollow class must change to follow, but not working.

        $('.unfollow').click(function(){

        $.ajax({
            type: "POST",
            url:"../ajax/settings/follow.php", 
            async: false,
            dataType:'html',
            data: {'id':$(this).attr('data-id')},
            success:function(rs){
               $(this).removeClass("unfollow"); 
                $(this).addClass("follow"); 

            },

            error:function (xhr, ajaxOptions, thrownError){
                alert("ERROR !!!");
                alert(xhr.status);
                alert(ajaxOptions);
                alert(thrownError);

            }  
        });   

    });

What’s wrong?

I apologize for my English!

  • 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-05T01:12:16+00:00Added an answer on June 5, 2026 at 1:12 am
     $('.unfollow').click(function(){
    
       // you need keep a reference of '.unfollow' here
       // otherwise you can't get it within success()
       // because success() is another scope
    
       var reference = this; 
    
       $ajax({
          ...
          success: function() {  
            $(reference ).removeClass("unfollow"); 
            $(reference ).addClass("follow"); 
            ....
          }
       });
      });
    

    and do same for your follow:

     $('.follow').click(function(){
    
       // you need keep a reference of '.unfollow' here
       // otherwise you can't get it within success()
       // because success() is another scope
    
       var reference = this; 
    
       $ajax({
          ...
          success: function() {  
            $(reference ).removeClass("follow"); 
            $(reference ).addClass("unfollow"); 
            ....
          }
       });
      });
    

    Here one important thing to note that, changing of class not done immediately with click, because you’re changing class within ajax success function, which will take some time to change the class. If you want to change the class instantly on click then take away those change statement outside of ajax success function.

    Another important note

    When you change the class of span then it becomes dynamic element to DOM. suppose you have span with class follow and on click event to span.follow you change the class to unfollow. As the .unfollow was not belong to DOM at page load so it becomes an dynamic element and on ordinary bind will not work here. Same for when you change class from unfollow to follow.

    So in both case you need delegate event handler (aka live) event handler like following:

    $(document).ready(function() {
        $('#container').on('click', 'span.follow', function() {
            var reference = this;
            alert('follow');
            $(reference).removeClass("follow");
            $(reference).addClass("unfollow");
        }).on('click', 'span.unfollow', function() {
            var reference = this;
            alert('unfollow');
            $(reference).removeClass("unfollow");
            $(reference).addClass("follow");
        });
    });
    

    DEMO

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

Sidebar

Related Questions

I have the code as per below. This retrieves some data to use for
I have this two lines of html code... <div id=slider1 data-param1=XXX data-param2=XXX></div> <script src=script.js
I am new to jquery mobile. I have one problem. I want to increase
i have one problem with my code ,i did a sample program to display
I have one problem , I want to get some data from XML file
I have an issue with this jquery code below. Please give me advice or
I am trying to use AJAX polling with jQuery to update a span element
I'm trying to use jQuery.post() function to retrieve some data. But i get no
I have one problem. I have to serialize an object and that sam object
I have one problem is that my HTMl body has a width of 1600px

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.