Change onClick attribute with javascript

后端 未结 6 1122
执笔经年
执笔经年 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:16

    The line onclick = writeLED(1,1) means that you want to immediately execute the function writeLED(arg1, arg2) with arguments 1, 1 and assign the return value; you need to instead create a function that will execute with those arguments and assign that. The topmost answer gave one example - another is to use the bind() function like so:

        var writeLEDWithSpecifiedArguments = writeLED.bind(this, 1,1);
        document.getElementById('buttonLED'+id).onclick = writeLEDWithSpecifiedArguments;
    

提交回复
热议问题