In jquery1.71, .live() is deprecated. Nevertheless, it still works
var toggleBtn = document.createElement('input');
toggleBtn.id = 'toggleBtn';
toggleBtn.type = 'button';
toggleBtn.value = 'Close';
box.appendChild(toggleBtn);
$('#toggleBtn').live('click', function() {
alert("hihi");
});
I tried the recommended .on(). Strangely, this fails
var toggleBtn = document.createElement('input');
toggleBtn.id = 'toggleBtn';
toggleBtn.type = 'button';
toggleBtn.value = 'Close';
box.appendChild(toggleBtn);
$('#toggleBtn').on('click', function() {
alert("hihi");
});
Make sure you read the documentation fully. For
.on()to work like.live()(or.delegate()) the second argument must be the selector to delegate to.You can use a more specific selector, of course.