Skip to content

Instantly share code, notes, and snippets.

@thecfguy
Created August 18, 2013 06:00
Show Gist options
  • Select an option

  • Save thecfguy/6260130 to your computer and use it in GitHub Desktop.

Select an option

Save thecfguy/6260130 to your computer and use it in GitHub Desktop.
Correct way pass additional variable to callback function to make sure correct value passed on
<html>
<head>
<script src="/js/jquery-1.4.2.min.js?131211022029" type="text/javascript"></script>
<script>
function loadMe(){
for(var i=1;i<=3;i++){
$.ajax({
url:"scribble2.cfm?id=" + i,
success:showMessage(i)
});
}
}
var showMessage = function(index) {
return function(res) {
$("#div" + index).html(res);
};
};
</script>
<style>div { height:40px; border:1px solid #000;margin: 5px 5px 5px 5px;}</style>
</head>
<body>
<a href="javascript:loadMe()">Load Div</a>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment