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

I'm wondering if this is possible. I have html like so: <p> <font face=Georgia>
I have this Chrome Extension, it adds a poke all button under your pokes.
I have an enormous file of HTML that I'd like to convert to one
I'm pretty sure that many people have thought of this, but for some reason
I have a container div element that has overflow:hidden on it. Unfortunately this property
As discussed a bit in this question , I am using Apache mod_include with
I'm just not getting event delegation with jquery ui tabs, or at all! I
I am using Microsoft's XAML/HTML converter to convert HTML from a database into a
I would like to implement a RTE (Rich Text Editor) with the ability to
I've been working on this for about a day and a half now, and

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.