最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing parameter onclick, in a loop - Stack Overflow

programmeradmin2浏览0评论

Im creating input elements in a loop. On click, these buttons are supposed to call a certain function with a certain Parameter, based on the Initial creation of the button element. What in fact does happen is that all Buttons always call the function with Parameter from the LAST button.

for (var i = 0; i < planets.length; i++){
  var id = planets[i].id;

  var input = document.createElement("input");
  input.type = "button";
  input.className = "worldButton";
  input.value = "Choose this world";
  Input.onclick = function(){
     game.pickWorld(planets, id);
  }
}

Each button always seems to pass on the id of planets[planets.length] instead of planets[i]. console.log(id) is correct. It doesnt matter if use onclick, addEventListener or jquery.click.

The whole planets Array however is passed on successfully.

How can i get the Input to correctly pass on the id of that particular Iteration ?

Im creating input elements in a loop. On click, these buttons are supposed to call a certain function with a certain Parameter, based on the Initial creation of the button element. What in fact does happen is that all Buttons always call the function with Parameter from the LAST button.

for (var i = 0; i < planets.length; i++){
  var id = planets[i].id;

  var input = document.createElement("input");
  input.type = "button";
  input.className = "worldButton";
  input.value = "Choose this world";
  Input.onclick = function(){
     game.pickWorld(planets, id);
  }
}

Each button always seems to pass on the id of planets[planets.length] instead of planets[i]. console.log(id) is correct. It doesnt matter if use onclick, addEventListener or jquery.click.

The whole planets Array however is passed on successfully.

How can i get the Input to correctly pass on the id of that particular Iteration ?

Share Improve this question edited May 27, 2015 at 8:25 Manashvi Birla 2,8433 gold badges15 silver badges28 bronze badges asked May 27, 2015 at 8:12 Christian FChristian F 2594 silver badges10 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 17

You need to use an IIFE and create a new closure like

for (var i = 0; i < planets.length; i++){
  var id = planets[i].id;

  var input = document.createElement("input");
  input.type = "button";
  input.className = "worldButton";
  input.value = "Choose this world";
  
  input.onclick = (function(id, planets){
     return function(){
         game.pickWorld(planets, id);
     }
  })(id,planets);
}

What was happening is the variables planets and id were visible to the callback function and at execution time, were using the last iterated value.

However, with a closure, you effectively created a 'private' variable seen and preserved by each callback function.

You need to pass the id and planets variables in the function like:

for (var i = 0; i < planets.length; i++){
  var id = planets[i].id;

  var input = document.createElement("input");
  input.type = "button";
  input.className = "worldButton";
  input.value = "Choose this world";
  Input.onclick = (function(pl, id){
     game.pickWorld(pl, id);
  })(planets, id);
}

You can read more about IIFE in this article.

发布评论

评论列表(0)

  1. 暂无评论