Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Event Keypress: Enter key

i have a combo box which has got values and i want to give the user to select the values when the Enter key pressed.

  1. User can navigate through Arrow key
  2. Select the value when user enters Enter key.

I have done this one :

$('#cmb_CIMtrek_DailyshipCo_CustomerName select').bind('keypress', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 13) { //Enter keycode
       //Do something
         alert("Enter key Pressed");
     }
});

but is not giving alert when I press Enter key.

What could be the problem and how to solve it?

Best Regards.

like image 750
Java Questions Avatar asked Apr 17 '26 06:04

Java Questions


2 Answers

 <select>
    <option value="1">1</option>
    <option value="2">2</option>
 </select>     

 <script> 
 $('select').live('keypress',function(e){
     var p = e.which;
     if(p==13){
         alert('enter was pressed');
     }
 });
 </script>
like image 67
catherine Avatar answered Apr 19 '26 18:04

catherine


Try this one

$('#cmb_CIMtrek_DailyshipCo_CustomerName select').keypress(function(event){

        var keycode = (event.keyCode ? event.keyCode : event.which);
        if(keycode == '13'){
            alert('You pressed a "enter" key in textbox');  
        }
        event.stopPropagation();
    });
like image 30
Varada Avatar answered Apr 19 '26 19:04

Varada