This is what I have:
<script language="javascript">
$('#click').one('click', function () {
var html = '<input type="text" class="input-mini" id="new-input" />';
$(this).parent().append(html);
});
$('#new-input').on('keyup', function (e) {
alert('A key was pressed');
if (e.keyCode == 13) {
alert('Enter key pressed');
}
});
</script>
At the recommendation of other SO answers, I found that I should use on()
to bind dynamically created elements. What I want to happen is for an AJAX call to occur after the user presses the Enter key inside the <input>
with ID new-input
. However, nothing happens when any key is pressed at all.
What do I need to do in order to bind the keyup
method to the newly-appended <input>
element?
Thanks