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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:23:56+00:00 2026-05-22T00:23:56+00:00

So i have all these links in html like this <a href=#ajax class=invlink competition_id=532>Gen

  • 0

So i have all these links in html like this

<a href="#ajax" class="invlink" competition_id="532">Gen Invoice</a>
<a href="#ajax" class="invlink" competition_id="534">Gen Invoice</a>
<a href="#ajax" class="invlink" competition_id="535">Gen Invoice</a>

then i wrote some javascript which binds to the click event
and i want it to submit an ajax request, and replace the anchor with the returned text.
but if i have clicked on multiple links so that several are running asychronously, then it doesn’t update all the anchors with the returned text, only the last anchor i clicked on.

i am guessing that the anchor variable is being overwritten each time it is run, how would i structure my code so that each time the click event is triggered, it updates the correct anchor on completion?

here is the javascript

<script type="text/javascript">

    $(document).ready(function() {
        // bind geninvoice function to all invlink's
        $('.invlink').bind('click', geninvoice);
    });


    function geninvoice() {

        // stop double clicks
        anchor = $(this);
        anchor.unbind('click');

        competition_id = $(this).attr('competition_id');

        $.ajax({
            type: "POST",
            url: "<?php echo url::site('manage/ajax/geninvoice'); ?>/"+competition_id,
            dataType: "json",
            beforeSend: function() {
                anchor.addClass("loading");
            },
            success: function(response, textStatus) {
                anchor.replaceWith(response.invoice_number);
            },
            error: function(response) {
                alert("Unknown Error Occurred");
                anchor.bind('click', geninvoice); // rebind if error occurs
            },
            complete: function() {
                anchor.removeClass("loading");
            }
        });
    }

</script>
  • 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-22T00:23:57+00:00Added an answer on May 22, 2026 at 12:23 am

    Yeah, the problem is that your anchor variable as it is written being ‘hoisted’ to a global scope. See this jsfiddle for a simplified example.

    You can fix this, by putting a var in front of the variable, so its scope will be limited to the function:

    function geninvoice() {
    
        // stop double clicks
        var anchor = $(this); //<-- put a var here
    

    You can see the fix at this updated version of the above fiddle

    Note, this will only help you for scoping within functions. The x variable in the following example will be hoisted to the top of the global scope even though it has been declared with a var:

    var a = 1;
    var b = 1;
    
    if (a === b){
       var x = 0;
    }
    
    alert(x); //alerts '0'
    

    the advantages of scoping within functions is on of the reasons we often see the following convention around jQuery plugins:

    (function($){
        //plugin code
        //all variables local to the invoked anonymous function
    })(jQuery);
    

    See at this JSFiddle

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

Sidebar

Related Questions

all. I have an huge html file which contains tags like these: <h3 class=r>
I have a long string that contains multiple HTML links that looks like this:
In my html page, i have links like this <table width=100% border=0 cellspacing=4 cellpadding=4>
I have a RouteLink constructed like so <p class=articleLink> @MvcHelper.Html.RouteLink(article.Title, Article_Route, new RouteValueDictionary() {
I want to redirect all links that have .html extension to a process.php file
So I have these lines of javascript code that I need to remove all
Say I have all these files in a folder: something(34324).php something(34).php something(3433454546).php something(65565).php something(9887).php
How can I do a count distinct selection? Basically I have all these charges
I have three arrays and I have copied all these arrays into a single
Let's suppose I have all of these already provided: The name of a table

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.