admin管理员组文章数量:1220423
var del = document.createElement('input');
del.type = 'button';
del.name = 'delll';
del.value = 'del';
del.onClick = 'alert("hi javascript")';
Here I have dynamically created a input type BUTTON and now I want to call function on button click event. I am using onClick();
function for this. In the above code all is working fine but del.onclick
is not working as I want (for generating alert for demo)
I am not using any jquery code in this program so please don't use any jquery code.
var del = document.createElement('input');
del.type = 'button';
del.name = 'delll';
del.value = 'del';
del.onClick = 'alert("hi javascript")';
Here I have dynamically created a input type BUTTON and now I want to call function on button click event. I am using onClick();
function for this. In the above code all is working fine but del.onclick
is not working as I want (for generating alert for demo)
I am not using any jquery code in this program so please don't use any jquery code.
Share Improve this question edited Jul 14, 2017 at 22:11 marcadian 2,61814 silver badges20 bronze badges asked Sep 16, 2013 at 6:30 user2750762user2750762 4192 gold badges6 silver badges25 bronze badges 1- Take a look at this answer stackoverflow.com/questions/6956258/… – Neeraj Commented Sep 16, 2013 at 6:35
4 Answers
Reset to default 7set 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
del.onclick = function () {
alert("hi jaavscript");
};
Use small "C"
in onClick
and pass a function for it.
Demo here
Try like this
var del = document.createElement('input');
del.setAttribute('type', 'button');
del.setAttribute('name', 'delll');
del.setAttribute('value', 'del');
del.setAttribute('onClick', 'alert("hi jaavscript")');
So to answer the question in details:
del.onclick = '' does not "execute" something within quotes. If you want to execute/call a function, you would want to pass the function as a parameter.
i.e
del.onclick = function() { alert('hi there!')}
本文标签: how to call onclick function on dynamically created button in javascriptStack Overflow
版权声明:本文标题:how to call onclick function on dynamically created button in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739244248a2154605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论