How do you assign a JavaScript 'onclick' attribute dynamically?

后端 未结 7 1247
说谎
说谎 2020-12-06 11:11

I\'m creating a button dynamically using JavaScript and at the same time assigning attributes such as \'ID\', \'type\' etc and also \'onclick\' in order to trigger a functio

7条回答
  •  爱一瞬间的悲伤
    2020-12-06 11:26

    In case this question is passed as a dupe, here is how to do it in current browsers

    ES6

    backButton.addEventListener("click",() => history.back());
    

    Older but newer than onclick

    backButton.addEventListener("click",function() { history.back() });
    

提交回复
热议问题