Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyboard navigation with JQuery not acting as expected
    primarykey
    data
    text
    <p>I followed the following tutorial which is supposed to allow keyboard scrolling and selecting through unordered list items -> <a href="http://www.marcofolio.net/webdesign/advanced_keypress_navigation_with_jquery.html." rel="nofollow">Tutorial</a></p> <p>I'm using it alongside the suggestion box as described -> <a href="http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html" rel="nofollow">Suggestion Box</a></p> <p>PLEASE NOTE THAT I FOLLOWED BOTH THESE TUTS TO THE LETTER WITH THE EXCEPTION OF CHANGING ID's and NAMES.</p> <p>It does seem to work (when I key down the hover effect does show), but it immediately disappears after I let go of the key. If I keep the down arrow pressed, it will scroll to the bottom of my list but once the key is released the hover effect disappears immediately. JQuery is by no means my strong suite so I'm not really sure what I'm doing wrong. I'm using the code exactly as described in the tutorial. I've only changed the #menu to #suggestions as this is what my div is named.</p> <p>Here is the code however:</p> <pre><code> var currentSelection = 0; var currentUrl = ''; google.load("jquery", "1.3.1"); google.setOnLoadCallback(function() { // Register keypress events on the whole document $(document).keypress(function(e) { switch(e.keyCode) { // User pressed "up" arrow case 38: navigate('up'); break; // User pressed "down" arrow case 40: navigate('down'); break; // User pressed "enter" case 13: if(currentUrl != '') { window.location = currentUrl; } break; } }); // Add data to let the hover know which index they have for(var i = 0; i &lt; $("#suggestions ul li a").size(); i++) { $("#suggestions ul li a").eq(i).data("number", i); } // Simulote the "hover" effect with the mouse $("#suggestions ul li a").hover( function () { currentSelection = $(this).data("number"); setSelected(currentSelection); }, function() { $("#suggestions ul li a").removeClass("itemhover"); currentUrl = ''; } ); }); function navigate(direction) { // Check if any of the menu items is selected if($("#suggestions ul li .itemhover").size() == 0) { currentSelection = -1; } if(direction == 'up' &amp;&amp; currentSelection != -1) { if(currentSelection != 0) { currentSelection--; } } else if (direction == 'down') { if(currentSelection != $("#suggestions ul li").size() -1) { currentSelection++; } } setSelected(currentSelection); } function setSelected(menuitem) { $("#suggestions ul li a").removeClass("itemhover"); $("#suggestions ul li a").eq(menuitem).addClass("itemhover"); currentUrl = $("#suggestions ul li a").eq(menuitem).attr("href"); } </code></pre> <p>Can anybody perhaps assist me in this matter please?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload