-1
rated 0 times
[
6]
[
7]
/ answers: 1 / hits: 15604
/ 11 Years ago, wed, november 13, 2013, 12:00:00
I need to create button dynamically and assign its onclick
handler. Click handler could be anonymous function (I'm not sure how it is called in JS). It is allowed to jQuery
.
I tried something like this:
<div>
<button id=x>Show</button>
</div>
function magick() {
console.log('some special magick');
}
function createButton(itsHandler) {
var guts = '<button id=__internal onclick='
+ itsHandler + // <-- that's wrong
'>Test</button>';
$($.trim(guts)).appendTo('body');
}
$(document).ready(function () {
$(#x).bind(click, function() {
createButton(magick);
});
});
but is doesn't work.
How it can be accomplished?
UPD1: It would be better if it was done inside of the createButton
function.
More From » jquery