javascript to create a button with onclick
89,168
function createButton(context, func) {
var button = document.createElement("input");
button.type = "button";
button.value = "im a button";
button.onclick = func;
context.appendChild(button);
}
window.onload = function() {
createButton(document.body, function() {
highlight(this.parentNode.childNodes[1]);
// Example of different context, copied function etc
// createButton(this.parentNode, this.onclick);
});
};
Is that what you want?
Author by
WindowsMaker
Updated on July 05, 2020Comments
-
WindowsMaker almost 4 years
I'm trying to use javascript to create a button that has a onclick event that calls a function defined in the head that takes in as parameter a dom object relative to the button. how do i do this?
ex:
<html> <head> <script>function blah(obj){alert(obj.value)}</script></head> <body> <button onclick="blah(this.parentNode.value);"></button> </body> </html>
javascript:
var newButton = document.createElement("button"); ???
in the end i want the new button to be the same as the existing one.