Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. div {width:300px;}
  2. a{display:block;}
  3. a:hover{background:#ccc;}
  4.  
  5. $(document).ready(function() {
  6. $('#locKeySearch').keydown(function(e)
  7. {<br>
  8. var alinks = $('#locDropDown').find('a');
  9. if(alinks.length > 0)
  10. {
  11. alinks.each(function(){
  12.  
  13. if (e.keyCode === 40)//Down Arrow
  14. {
  15. e.preventDefault();
  16. var current = alinks.index(),
  17. next = $(this).next();
  18. this.blur();
  19. setTimeout(function() { next.focus().select(); }, 50);
  20. }
  21. });
  22. }
  23. });
  24. });
  25.  
  26. <form>
  27. <input type="text" name="locKeySearch" id="locKeySearch" value="" />
  28. </form>
  29. <div id="locDropDown">
  30. <a href="1">1</a>
  31. <a href="2">2</a>
  32. <a href="3">3</a>
  33. <a href="4">4</a>
  34. <a href="5">5</a>
  35. <a href="6">6</a>
  36. <a href="7">7</a>
  37. <a href="8">8</a>
  38. <a href="9">9</a>
  39. <a href="10">10</a>
  40. </div>
  41.  
  42. function keyEvents()
  43. {
  44. var keyindex,alinks;
  45. keyindex = -1;
  46. // Key Events
  47. $('#locKeySearch').keydown(function(e){
  48. alinks = $('#locDropDown').find('a');
  49. if(alinks.length == 0)
  50. {
  51. keyindex = -1;
  52. }
  53. if(e.keyCode == 40)
  54. {
  55. e.preventDefault();
  56. if(alinks.length > 0 && keyindex == -1)
  57. {
  58. keyindex = 0;
  59. $('#locDropDown').find('a')[keyindex++].focus();
  60. }
  61. }
  62. });
  63.  
  64. $('#locDropDown').keydown(function(e)
  65. {
  66. alinks = $('#locDropDown').find('a');
  67. if(e.keyCode == 40)
  68. {
  69. e.preventDefault();
  70. if(keyindex == -1)
  71. {
  72. keyindex = 1;
  73. }
  74. if(alinks.length > 0 && keyindex < alinks.length)
  75. {
  76. $('#locDropDown').find('a')[keyindex++].focus();
  77. }
  78. }
  79. if(e.keyCode == 38)
  80. {
  81. e.preventDefault();
  82. if(keyindex == alinks.length)
  83. {
  84. keyindex = keyindex-2;
  85. }
  86.  
  87. if(alinks.length > 0 && keyindex < alinks.length && keyindex >=0)
  88. {
  89. $('#locDropDown').find('a')[keyindex--].focus();
  90. }
  91. }
  92. });
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement