Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  23] [ 6]  / answers: 1 / hits: 92582  / 13 Years ago, wed, december 28, 2011, 12:00:00

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.


More From » dom

 Answers
78
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?


[#88347] Monday, December 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristopher

Total Points: 58
Total Questions: 103
Total Answers: 102

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
;