Alon Shmiel
Alon Shmiel

Reputation: 7121

When backspace is pressed, delete the key or the li

I made the next jsfiddle: http://jsfiddle.net/alonshmiel/Sq5wa/4/

when the user press the backspace key, I want to delete by the next instructions:

  1. if the desired char to delete is a character, delete it.
  2. if the desired char is <li> element: delete the <li> and delete a <div> element that is found in "countries" <ul>: <div><br></div> (every <li> creates <div><br></div>)

I have a function that detects the backspace key:

$('#countries').keydown(function(e) {
    if (e.which == 8) {

    }
});

you can add li-s by add text to the ul and press the enter key

any help appreciated!

Upvotes: 2

Views: 658

Answers (1)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33880

You can do it with this line :

$(window.getSelection().anchorNode).closest('li').remove();

It will get the current cursor position and delete the node if it is a li.

Upvotes: 1

Related Questions