how to call onclick function on dynamically created button in javascript

前端 未结 4 1065
花落未央
花落未央 2021-02-14 21:45
var del = document.createElement(\'input\');
del.type = \'button\';
del.name = \'delll\';
del.value = \'del\';
del.onClick = \'alert(\"hi  javascript\")\';
4条回答
  •  抹茶落季
    2021-02-14 22:30

    set the onclick (all lower case) to an anonymous function

    del.onclick = function(){ alert('hi javascript');};
    

    note the function is not in quotes like other attributes

提交回复
热议问题