BobbyJones
BobbyJones

Reputation: 1354

Removing the selected LI from a UL with the onkeydown event: delete button

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

Answers (2)

swestfall
swestfall

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

gllen
gllen

Reputation: 66

  1. The refdocs_list doesn't receive the keyup, changing it to body for example would do it

    $("body").on('keyup', function(e) {

  2. There is no $ ahead of the line inside the if statement

    $('ul li.selected').remove()

Upvotes: 2

Related Questions