JavaScript event handler arguments

后端 未结 1 1249
情深已故
情深已故 2021-01-18 07:50

I have the following JavaScript code:

var ans_el = document.createElement( \'input\' );
ans_el.setAttribute( \'id\', unique_int_value );
ans_el.setAttribute(         


        
相关标签:
1条回答
  • 2021-01-18 08:41

    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);
    };
    
    0 讨论(0)
提交回复
热议问题