Change onClick attribute with javascript

后端 未结 6 1119
执笔经年
执笔经年 2020-12-02 16:35

This is my function and it should change the onClick attribute of the HTML input, but if I use

document.getElementById(\'buttonLED\'+id).onclick = \"writeLED         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 17:31

    Another solution is to set the 'onclick' attribute to a function that returns your writeLED function.

    document.getElementById('buttonLED'+id).onclick = function(){ return writeLED(1,1)};
    

    This can also be useful for other cases when you create an element in JavaScript while it has not yet been drawn in the browser.

提交回复
热议问题