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 that binds to the click event and I want it to send an ajax request and replace the anchor with the returned text. but if I clicked on several links so that several were executed asynchronously, then it does not update all the anchors with the returned text, but only the last anchor that I clicked on.
I assume that the binding variable is overwritten every time it starts, how would I structure my code so that every time the click event is fired, it updates the correct anchor at the end?
here is javascript
<script type="text/javascript">
$(document).ready(function() {
$('.invlink').bind('click', geninvoice);
});
function geninvoice() {
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);
},
complete: function() {
anchor.removeClass("loading");
}
});
}
</script>
source
share