Reputation: 1354
I am attempting to remove the selected LI (who's classname is 'selected') from my UL using the onkeydown event: delete button (46). It doesn't seem to remove the LI at all. What am I doing wrong?
Here is a fiddle:
The code in question:
$("#refdocs_list").on('keyup', function(e) {
alert(e.which)
if (e.which == 46) {
('ul li.selected').remove()
}
});
Upvotes: 0
Views: 361
Reputation: 411
I had to attach the press to the document level and fix a couple of missing semicolons to get it to work. Here is the modified javascript:
$('#refdocs_list').on('click', 'li', function(e) {
var $this = $(this);
$this.addClass('selected').siblings().removeClass('selected');
document.getElementById('refdocs').value = $this.text();
});
$(document).on('keyup', function(e) {
console.log(e.which);
if (e.which == 46) {
$('ul li.selected').remove();
}
});
Upvotes: 1
Reputation: 66
The refdocs_list doesn't receive the keyup, changing it to body for example would do it
$("body").on('keyup', function(e) {
There is no $ ahead of the line inside the if statement
$('ul li.selected').remove()
Upvotes: 2