Michal Michal - 3 months ago 8
jQuery Question

Can't get keyup event to fire on an icon

$(document).ready(function() {
$('#FeedbackCloseIcon').click(function(e) {
console.log('clicked icon');
});

$('#FeedbackCloseIcon').on('keyup', function (event) {
console.log('button on icon');
if(event.which === 13){
console.log('enter on icon');
}
});
});


The issue I am having is the keyup event for the FeedbackCloseIcon not firing. The click event works fine.

<button class="dialog-close" aria-label="close" href="#" type="button">
<span class="icon icon-close" id="FeedbackCloseIcon">x</span>
</button>


If I change the object for the keyup event to

<a class="dialog-cancel" id="cancel-feedback" href="javascript:void(0)">Cancel</a>


It works fine

$('#cancel-feedback').on('keyup', function (event) {
console.log('button on icon');
if(event.which === 13) {
console.log('enter on icon');
}
});

Answer

Span keyup/keydown Or any key event not register in DOM, you have to get it's parent mean Button keyup/keydown/keypress event.

    $(".dialog-close").keypress(function (e) {            
        console.log('enter on icon');
    });
Comments