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 7522805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:37:14+00:00 2026-05-30T02:37:14+00:00

Please, I need your help. I have a Follow button. When the button is

  • 0

Please, I need your help.
I have a “Follow” button. When the button is pressed, javascript click event is triggered and then some ajax requests are made. If all ok, then the “Follow” button needs to be replaced with “Unfollow” button with the proper code. So far I can only change buttons text.. but it’s not good, because if user want to follow and then unfollow (without page refresh), my code will not work.

Here is my html for Follow button:

<a href="javascript:;" class="genbutton btn-follow" id="follow-239618"><span>Follow</span></a>

Here is for Unfollow button:

<a href="javascript:;" class="genbutton btn-unfollow" id="unfollow-239618"><span>Unfollow</span></a>

Here is my javascript:

    function sub(subAction, user_id) {                                                                                                              
    if(subAction == 'follow'){                                                                                                                  
        $("a#follow-"+ user_id +" span").addClass("loading");                                                                                   
    }                                                                                                                                           
    if(subAction == 'unfollow'){                                                                                                                
        $("a#unfollow-"+ user_id +" span").addClass("loading");                                                                                 
    }                                                                                                                                           
    $.ajax({                                                                                                                                    
        type: "post",                                                                                                                           
        url: "http://"+ siteDomain +"/a/subHandler",                                                                                            
        data: "do="+ subAction +"&user_id=" + user_id,                                                                                          
        success: function(data, textStatus){                                                                                                    
            if(subAction == 'follow'){                                                                                                          
                $("a#follow-"+ user_id +" span").removeClass("loading");                                                                        
            }                                                                                                                                   
            if(subAction == 'unfollow'){                                                                                                        
                $("a#unfollow-"+ user_id +" span").removeClass("loading");                                                                      
            }                                                                                                                                   
            var errorOccured = data["ERR"];                                                                                                     
            if(!errorOccured){                                                                                                                  
                if(subAction == 'follow'){                                                                                                      
                    $("a#follow-"+ user_id +" span").text("Unfollow");                                                                          
                }                                                                                                                               
                if(subAction == 'unfollow'){                                                                                                    
                    $("a#unfollow-"+ user_id +" span").text("Follow");                                                                          
                }                                                                                                                               
            }                                                                                                                                   
        }                                                                                                                                       
    }, "json");                                                                                                                                 
}                                                                                                                                               
$(".btn-follow").click(function(){var user_id = $(this).attr("id").replace('follow-', '');sub('follow', user_id);return false;});               
$(".btn-unfollow").click(function(){var user_id = $(this).attr("id").replace('unfollow-', '');sub('unfollow', user_id);return false;});         
  • 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-05-30T02:37:15+00:00Added an answer on May 30, 2026 at 2:37 am

    Two suggestions:

    One: The following works without jquery. There may be better ways to do it (there may even be some jquery magic to achieve the same in much less lines of code) but this is how I would do it. You can simply copy and paste it to a new file and see it work. This snippet removes the old button and then adds a new button.

    <html>
    <head>
        <title>unfollow example</title>
        <script type="text/javascript">
            function switchFollow() 
            {
                var buttonParent = document.getElementById('PARENTCONTAINEROFBUTTON');
                var oldButton = document.getElementById('FOLLOWBUTTON');
                buttonParent.removeChild(oldButton);
                var newButton = document.createElement('a');
                newButton.setAttribute('id', 'unfollowbutton');
                newButton.setAttribute('href', '#unfollow');
                var newButtonText = document.createElement('span');
                newButtonText.innerHTML = "unfollow";
                newButton.appendChild(newButtonText);
                buttonParent.appendChild(newButton); 
                }   
        </script>
    </head>
    <body>
        <div id="PARENTCONTAINEROFBUTTON">
            <a id="FOLLOWBUTTON" href="#follow"><span>follow</span></a>         
        </div>
        <div id="othercontainer">
            <button onclick="switchFollow()">switch to unfollow</button>
        </div>
    </body>
    

    Two: If you feel that this remove and add could cause you performance issues (don’t know how many buttons total we are talking about and what your target platform is) check out the second snippet. If you give your span an id you can change the text and the href without remove and add.

    <html>
    <head>
        <title>unfollow example 2</title>
        <script type="text/javascript">
            function switchFollow() 
            {
                button = document.getElementById('FOLLOWBUTTON');
                button.setAttribute('id', 'UNFOLLOWBUTTON');
                button.setAttribute('href', '#unfollow');
                buttonText = document.getElementById('buttontext');
                buttonText.innerHTML = "unfollow";
                }   
        </script>
    </head>
    <body>
        <div id="PARENTCONTAINEROFBUTTON">
            <a id="FOLLOWBUTTON" href="#follow"><span id="buttontext">follow</span></a>         
        </div>
        <div id="othercontainer">
            <button onclick="switchFollow()">switch to unfollow</button>
        </div>
    </body>
    

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

Sidebar

Related Questions

Need some help, please. I have a line of horizontal thumbnails loaded as ONE
please I need your help with a Linq expression: I have nested objects with
I need some help! Please. I am trying to develop a very very simple
I need some with help with PowerShell, please. It should be pretty easy: I
I am beginner on WPF and need your help. Problem: I have 4 buttons
I need your help in doing the ajax part to refresh the div on
Hi, ive been dealing with this for some time now and need your help.
I really need your help since I have a frustrating problem. I'm downloading data
I really need your help. I have a database with latitude and longitude data,
i need again your help... I have this php code: <? if(isset($_POST['addnews'])){ $type =

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.