Tuesday, July 31, 2012

html – JavaScript passing arguments to button onclick functions



I\’m trying to create a bunch of buttons via loop, and use the loop iterator as an argument for each of the button onclick functions.


My code:



var testFnc = function(i) { alert(\"Arg: \" + i); }

mainDiv.append(create_button(name, \"buttonCSS\", testFnc(i)));


However the functions are called automatically as the page loads and the buttons are placed (i.e. I see the alerts right away).


I\’m sure there\’s some common design pattern for this.


Thanks!






Rating: 2 out of 5 based on 3 ratings



The post html – JavaScript passing arguments to button onclick functions appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/html-javascript-passing-arguments-to-button-onclick-functions.html

No comments:

Post a Comment