Posted By

nerdfiles on 09/17/10


Tagged

javascript jquery jumping


Versions (?)

jQuery JumpTo


 / Published in: jQuery
 

When the maxlength is hit, jumps to the next form field. Just put the select on all the relevant form elements, even the last one.

  1. ;(function($) {
  2.  
  3. var a = [];
  4.  
  5. $.jumpTo = function(c) {
  6.  
  7. return $(c).each(function(index, e) {
  8.  
  9. a.push($(this));
  10.  
  11. var ml = $(this).attr("maxlength"),
  12. counter = 0;
  13.  
  14. $(this).bind("keyup", function(e) {
  15. counter = $(this).val().length;
  16.  
  17. if (ml <= counter) {
  18. counter = 0;
  19. for (var i = 0; i < a.length; i++) {
  20. if ($(this).attr("id") == a[i].attr("id")) {
  21. if (a[i+1] &&
  22. e.keyCode !== 9 &&
  23. e.keyCode !== 16 &&
  24. e.keyCode !== 40 &&
  25. e.keyCode !== 39 &&
  26. e.keyCode !== 38 &&
  27. e.keyCode !== 37) { // tab, shift, arrows
  28. a[i+1].focus();
  29. }
  30. }
  31. }
  32. }
  33. counter++;
  34. });
  35.  
  36. });
  37.  
  38. };
  39.  
  40. })(jQuery);
  41.  
  42.  
  43. // I use the following:
  44. $.jumpTo("input.jump-to");

Report this snippet  

You need to login to post a comment.