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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:48:59+00:00 2026-06-04T14:48:59+00:00

Sorry if the title is not clear enough, I have <a> elements with the

  • 0

Sorry if the title is not clear enough, I have <a> elements with the same class when user clicks on each one the jquery code should run for each one alone, the problem is when clicking the first element and before it finishes running the whole code if I click a second element it leaves the first one incomplete and runs the code on the second one, it’s supposed to work simultaneously (btw it’s supposed to send data to a php file and it’s working fine even if I click many elements by the classes are not working)

here’s the code:

    jQuery(function($) {
        $('.anchor-tag-class').each(function() {
            $(this).click(function() {
                $this = $(this);
                var id = $this.attr('data-id');
                if ( $this.hasClass('on') ) {
                    $this.removeClass('on');
                    $this.addClass('loading');
                    $.post("process.php", { element_id: id, status: 'off' }, function(data) { $this.addClass('off').removeClass('loading'); } );
                } else {
                    $this.removeClass('off');
                    $this.addClass('loading');
                    $.post("process.php", { element_id: id, status: 'on' }, function(data) { $this.addClass('on').removeClass('loading'); } );
                }
            });
        });
    });

so what am I doing wrong ?

and thanks in advance.

  • 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-04T14:49:00+00:00Added an answer on June 4, 2026 at 2:49 pm

    You don’t need the each() in this case, as the code will only be run for the specific element which raised the event. You can also chain your functions hanging off the $this variable.

    Try this:

    $('.anchor-tag-class').click(function() {
        var $this = $(this);
        var id = $this.attr('data-id');
        if ( $this.hasClass('on') ) {
            $this.removeClass('on').addClass('loading');
            $.post("process.php", { element_id: id, status: 'off' }, function(data) { $this.addClass('off').removeClass('loading'); } );
        } 
        else {
            $this.removeClass('off').addClass('loading');
            $.post("process.php", { element_id: id, status: 'on' }, function(data) { $this.addClass('on').removeClass('loading'); } );
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am not sure if my title is clear enough. I essentially have
Sorry the title is not very clear. This is a follow up to my
Not sure the title fully describes the problem/question I'm trying to ask, sorry. One
Sorry if my title is not clear, I'm not sure how to word this.
first of all sorry for the title. I know this is not so clear
Not sure if I'm being too clear with the title. Sorry, it's late and
Sorry for the title, not easy to phrase it correctly. I have a table
Sorry if title is not too clear but I think it's about right. NEhow,
Sorry for the lame title: I'm not sure how to sum this one up...
Sorry if the title is not clear but I ll explain now my problem.

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.