How can I map a key press to hyperlinks in my HTML document using JavaScript? If I press 'a' it should go to and if I press 'b' it should go to

John Zukowski

You need to tell the document what method to call on key presses. The following demonstrates this by uses 'f' to go to the jGuru FAQs pages and 'j' for Jobs.

  var hotKeys = new Array("f","j");
  var urls = new Array("",

  if( hotKeys.length != urls.length )
    alert("Bad key/URL lists");

  if (document.layers)

  function navigate(e) {
    var enteredKey;
    var arrayKey;

    if (document.layers)
      enteredKey = String.fromCharCode(e.which);
    else if (document.all)
      enteredKey = String.fromCharCode(event.keyCode);

    enteredKey = enteredKey.toLowerCase()

    for(i = 0, n = hotKeys.length; i < n; i++) {
      arrayKey = hotKeys[i].toLowerCase();
      if (arrayKey == enteredKey) {

F for FAQs and J for Jobs
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact