Unable to type anything inside an input text box, after putting a jquery code for keycode trapping -


i have simple html form yes or no button. requirement press "y" or "n" keys , invoke corresponding button without focus.i have written jquery , on pressing 'y' or 'n' invoking correct button , working expected here 1 glitch.

my input text box not able accept text or in other words keys not working.

my html code :

<table>     <tr>          <td>username:</td> <td><input type="text" id="uname"> </td>      </tr>     <tr>          <td>password:</td> <td><input type="text" id="pswd"> </td>      </tr> </table> <br><br><br><br> <table  cellspacing="20">        <tr>         <td><button type="button" id="yes" >yes</button></td>&nbsp;&nbsp;         <td><button type="button" id="no"  >no</button></td>     </tr> </table> 

and jquery :

$(document).ready(function(){     $(document).keydown(function(e) {            e.preventdefault();            if (e.which == 89) {             alert("y");         }          if (e.which == 78) {             alert("n");         }     }); }); 

what reason?

i have updated code. need remove or comment e.preventdefault();

$(document).ready(function(){      $(document).keydown(function(e) {                           if (e.which == 89) {              alert("y");          }            if (e.which == 78) {              alert("n");          }      });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <table>      <tr>           <td>username:</td> <td><input type="text" id="uname"> </td>       </tr>      <tr>           <td>password:</td> <td><input type="text" id="pswd"> </td>       </tr>  </table>  <br><br><br><br>  <table  cellspacing="20">         <tr>          <td><button type="button" id="yes" >yes</button></td>&nbsp;&nbsp;          <td><button type="button" id="no"  >no</button></td>      </tr>  </table>


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -