Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Navigating using Cursor Keys

Navigating using Cursor Keys

Scheduled Pinned Locked Moved Web Development
helpcsharpjavascriptphpalgorithms
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    il_manti
    wrote on last edited by
    #1

    Hi all. I'm having a slight problem to which I can't seem to find a solution on Google (obv because I'm not searching right) so I thought you might point me towards a link with a solution. I have a textbox. Using Javascript, whenever the user enters a key, javascript iterates through a predefined array and returns a list of matching items in a Div. The result is :

    • item 1
      ......* item n

    Then the user can click on one of the items and the selected option is automatically inserted into the textbox. So far so good. But, how is it possible to let the user use the cursor keys on his/her keyboard to select an option? :/ I have no idea how can this be done but I'm sure it can be. Could someone direct me to a tutorial on this? Coz I'm sure it wouldn't make sense to give the code here (sure it will be too long). Thanks a lot for any help provided. PS: I'm using ASP VB but I can translate from PHP and .NET code if need be. I'm sure this is purely Javascript but still ;) In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

    S 1 Reply Last reply
    0
    • I il_manti

      Hi all. I'm having a slight problem to which I can't seem to find a solution on Google (obv because I'm not searching right) so I thought you might point me towards a link with a solution. I have a textbox. Using Javascript, whenever the user enters a key, javascript iterates through a predefined array and returns a list of matching items in a Div. The result is :

      • item 1
        ......* item n

      Then the user can click on one of the items and the selected option is automatically inserted into the textbox. So far so good. But, how is it possible to let the user use the cursor keys on his/her keyboard to select an option? :/ I have no idea how can this be done but I'm sure it can be. Could someone direct me to a tutorial on this? Coz I'm sure it wouldn't make sense to give the code here (sure it will be too long). Thanks a lot for any help provided. PS: I'm using ASP VB but I can translate from PHP and .NET code if need be. I'm sure this is purely Javascript but still ;) In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

      S Offline
      S Offline
      sph3rex
      wrote on last edited by
      #2

      take the following as an example var cur_elem = -1; function resetElements(prt,cls,omit) { for(i=0;i<=prt.childNodes.length -1; i++){ if(i != omit) { prt.childNodes[i].className = cls; } } } function dosomething(e) { var code; if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; target_elem = (!document.all) ? document.getElementById("testId") : document.all["testId"]; if(code == 38) { if(cur_elem >= 1) cur_elem -=1; target_elem.childNodes[cur_elem].className = "over"; resetElements(target_elem,"normal", cur_elem); target_elem.className = "activeList"; document.getElementById("notice").innerHTML = "Up:" + cur_elem + " .::. Element content:" + target_elem.childNodes[cur_elem].innerHTML + " .::. Total elements:" + target_elem.childNodes.length; } else if(code==40) { if(cur_elem <= target_elem.childNodes.length-2) cur_elem +=1; target_elem.childNodes[cur_elem].className = "over"; resetElements(target_elem,"normal", cur_elem); target_elem.className = "activeList"; document.getElementById("notice").innerHTML = "Down:" + cur_elem + " .::. Element content:" + target_elem.childNodes[cur_elem].innerHTML + " .::. Total elements:" + target_elem.childNodes.length; } } with defined styles like .normal { background-color:#ffffff; display:block; } .over { background-color:#990000; display:block; } .activeList {display:block} ul.def {display:none;} and with the following html:

      *   some value
      
      • some value 2
      • some value 3
      • some value 4
      • some value 5
      • some value 6

      [empty]

      be aware of the fact that the* elements MUST have no space between because of sum reason i dun understand(spaces are treated are child elements of the UL element in FF and opera.) This is a basic example ... further enhancements are up to you. Code? Yeah i love it fried together with a glass of wine.

      I 1 Reply Last reply
      0
      • S sph3rex

        take the following as an example var cur_elem = -1; function resetElements(prt,cls,omit) { for(i=0;i<=prt.childNodes.length -1; i++){ if(i != omit) { prt.childNodes[i].className = cls; } } } function dosomething(e) { var code; if (!e) var e = window.event; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; target_elem = (!document.all) ? document.getElementById("testId") : document.all["testId"]; if(code == 38) { if(cur_elem >= 1) cur_elem -=1; target_elem.childNodes[cur_elem].className = "over"; resetElements(target_elem,"normal", cur_elem); target_elem.className = "activeList"; document.getElementById("notice").innerHTML = "Up:" + cur_elem + " .::. Element content:" + target_elem.childNodes[cur_elem].innerHTML + " .::. Total elements:" + target_elem.childNodes.length; } else if(code==40) { if(cur_elem <= target_elem.childNodes.length-2) cur_elem +=1; target_elem.childNodes[cur_elem].className = "over"; resetElements(target_elem,"normal", cur_elem); target_elem.className = "activeList"; document.getElementById("notice").innerHTML = "Down:" + cur_elem + " .::. Element content:" + target_elem.childNodes[cur_elem].innerHTML + " .::. Total elements:" + target_elem.childNodes.length; } } with defined styles like .normal { background-color:#ffffff; display:block; } .over { background-color:#990000; display:block; } .activeList {display:block} ul.def {display:none;} and with the following html:

        *   some value
        
        • some value 2
        • some value 3
        • some value 4
        • some value 5
        • some value 6

        [empty]

        be aware of the fact that the* elements MUST have no space between because of sum reason i dun understand(spaces are treated are child elements of the UL element in FF and opera.) This is a basic example ... further enhancements are up to you. Code? Yeah i love it fried together with a glass of wine.

        I Offline
        I Offline
        il_manti
        wrote on last edited by
        #3

        sweet ... thanks for the detailed reply man

        In life truth does not matter. What really matters is what others believe to be the truth. (The Up and Comer - Book)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups