Set Focus To Next Text Input On Press Enter Button

/ Published in: jQuery
Save to your folder(s)

Set focus to all next text input in a html document when press enter button on your keyboard. If focused at the last text input element, it will be back to the first text input element.

Copy this code and paste it in your HTML
  1. $('input[type=text]').keydown(function(e){
  2. //get the next index of text input element
  3. var next_idx = $('input[type=text]').index(this) + 1;
  5. //get number of text input element in a html document
  6. var tot_idx = $('body').find('input[type=text]').length;
  8. //enter button in ASCII code
  9. if(e.keyCode == 13){
  10. if(tot_idx == next_idx)
  11. //go to the first text element if focused in the last text input element
  12. $('input[type=text]:eq(0)').focus();
  13. else
  14. //go to the next text input element
  15. $('input[type=text]:eq(' + next_idx + ')').focus();
  16. }
  17. });

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.