0

I want detect when user use Entry. I want this, because I want custom the action made by the key. The key will be add a new row on my table. Can you add me please?

Thanks.

EDIT :

This solution don't work. I try all answers and any solution work ... :

<script type="text/javascript">
        function stopEntryKey(evt) {
            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
                if ((evt.keyCode == 13) && (node.type == "text")) { 
                    return false;
                    $("tbody").html("<tr><td>Item1-col1</td><td>Item1-col2</td></tr>"); // Ajoute une ligne
                }
        }
        document.onkeypress = stopEnterKey;
    </script>

EDIT 2 :

$(document).ready(function() {
$(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });
});

This code work. Find here : Prevent users from submitting a form by hitting Enter

Now, I search to add a row when user press the Entry key. Who know how please?

Community
  • 1
  • 1
Yvan1263
  • 80
  • 2
  • 14
  • Use `$("tbody").append("html of row")` – Mohammad Sep 12 '16 at 12:41
  • @Mohammad : Look this : [link]http://stackoverflow.com/questions/39450576/add-row-html-jquery – Yvan1263 Sep 12 '16 at 12:42
  • @Mohammad : I test this but he add a line but how to set my html file for one row? I have a HTML file for code one row, and I want when user use ENtry key to create a row with this HTML file. – Yvan1263 Sep 12 '16 at 12:45
  • [jQuery: load txt file and insert into div](http://stackoverflow.com/questions/6470567/jquery-load-txt-file-and-insert-into-div) – Mohammad Sep 12 '16 at 12:47

3 Answers3

0

You need to add key event listener to detecting key press. You can use javascript .addEventListener() or jquery .keydown() to adding keydown event listener. Then you should check if Enter is pressed, run your target code.

// Javascript
document.addEventListener("keydown", function(){
    if (event.keyCode == '13') {
        alert();
    }
});
// Javascript shortcut
document.onkeydown = function() {   
    if (event.keyCode == "13") {
        alert();
    }
} 
// JQuery
$(document).keydown(function() {   
    if (event.keyCode == '13') {
        alert();
    }
}); 

window.onkeydown = function() {   
    if (event.keyCode == '13') {
      alert("Enter key pressed");
    }
} 
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • That "Javascript shortcut" is awful and should not be used. It will break as soon as you have more than one of them since it'll overwrite the previous one. – ThiefMaster Sep 12 '16 at 09:46
  • @ThiefMaster I provide three way to do work. This does not mean you should use all three ways together. – Mohammad Sep 12 '16 at 09:51
  • You provide three ways, and one of which is quite bad and is better not to be recommended to people who might not know the drawbacks. – ThiefMaster Sep 12 '16 at 09:51
  • @ThiefMaster I can't understand your mean. Why `onkeydown` is *quite bad*? – Mohammad Sep 12 '16 at 09:53
  • imagine you have foo.js using `document.onkeydown = ...;` and bar.js also using it. now whatever is executed last will *replace* the other one – ThiefMaster Sep 12 '16 at 09:56
  • @ThiefMaster If what your said be right, It's not related to this question. Because question doen't use multiple js file or it does not mentioned in it. If you downvote my answer for the reason, it isn't right. – Mohammad Sep 12 '16 at 10:02
0

Let's say the HTML is as follow

<table>
     <thead>
        <tr>
          <th>Col1</th>
          <th>Col2</th>
        </tr>
     </thead>

     <tbody>
     </tbody>
</table> 

Now the JS:

$(document).keydown(function() {   
   if (event.keyCode == '13') {
       $("tbody").html("<tr><td>Item1-col1</td><td>Item1-col2</td></tr>";  //this 'll add row in the table
    }
}); 
KOUSIK MANDAL
  • 2,002
  • 1
  • 21
  • 46
-1

try this:

document.onkeydown = function(e) {   
  //something code
  //for something specific key:
  if (e.keyCode == 'someting_code_key') {
    // action
  }
} 

key code list

Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24