0

I have newly created table and I want to bind key-press event to td

I have written the following code and it is not working

$("body").on("keypress",".control table tr td",function(e){
    alert(9);
});

and I replaced keypress with click then it is working

$("body").on("click",".control table tr td",function(e){
    alert(9);
}); 
Your Yummy
  • 153
  • 1
  • 6
  • 20

2 Answers2

0

Per this answer you might need to set the parenting element's contentEditable="false" (not sure about your document structure, but maybe tr?). You might also try setting the contentEditable explicitly for the td element.

Community
  • 1
  • 1
Oiva Eskola
  • 949
  • 8
  • 13
0

Actually, as written in W3C, onkeypress event can occur on every HTML element, but not <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

You still need to have focusable tag. So, in order to do that to a td, you have to use tabindex attribute.

Based on those two links, you can do something like this:

<!-- this is td that needs to be focused in order to get onkeypress event -->
<td tabindex="1"></td>
Kaarel
  • 170
  • 1
  • 12