McNavy McNavy - 2 months ago 7
HTML Question

Detect "Entry key" press for custom function

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 work but I search now, for add a new line for my table when user press Entry key :

<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>


But this line : $("tbody").html("Item1-col1Item1-col2");` // Ajoute une ligne don't add one row for my table.

Answer

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
    }
}); 
Comments