I have the following JavaScript code:
var ans_el = document.createElement( \'input\' );
ans_el.setAttribute( \'id\', unique_int_value );
ans_el.setAttribute(
You need to give a reference to a function for onclick; you are currently executing the function and assigning that result to the onclick handler. This is closer to what you want:
ans_el.onclick = function(e) {
myFunction(ans_el.id, ans_el.value);
};
UPDATED: Decided to use event.target for a clearer example since Andir brought it up.
ans_el.onclick = function(e) {
myFunction(e.target.id, e.target.value);
};