1

In jQuery, how do you make it so when I click a div, I can press the delete key and make something happen?

For example, I click on a div, then after that I press the button delete and it alerts a message. If I dont click on the div and press delete, nothing happens. Thanks!!

anthonypliu
  • 12,179
  • 28
  • 92
  • 154

2 Answers2

1

Didn't test this code, but i imagine you should use something like this...

  var divSelected = null;
  $(document).ready(function(){
    $("div").click(
      function(){
        divSelected = caller.id;
        alert(test.val());
    }
  );
  $('#DIV').keypress(function(e){
      if(e.which == 46 && divSelected){ // 46 is the keycode for the delete key...
          // do what you want...
      }
   });
});
Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
0

Garis Suero is close, but I think it should be:

  $(function(){
  var divSelected = null;
    $('div').click(
      function(){
        divSelected = this;
    });
  $('div').keypress(function(e){
      if(e.which == 46 && this == divSelected){ 
        alert('Delete me!');
      }
   });
});

Also totally untested though ;)

Matt Evanoff
  • 966
  • 4
  • 9