Using arrow keys to navigate between elements


I have implemented a script for autosuggestion through AJAX and php on na input type text box.
The elements that appears are below the input box and appears in a <div> and each line is made as <ul> <li>line1</li> <li>line 2 etc</li> </ul>

I wish to navigate with arrow up and arrow down between each line. How can this be done?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What you have to do is have a variable that tracks on what line the cursor is on then you have to trap the keystroks and that increament of decrement the cursor traker and update the interface accordingly.
What about something like the following?
    <title>Test Page</title>
    <style type="text/css">
        #item_selector li {
            background-color: transparent;
            color: black;
            font-weight: normal;
        #item_selector li.selected {
            background-color: red;
            color: black;
            font-weight: bold;
    <script type="text/javascript">
        var selectedIndex = 0, itemCount = 0;
        function keyPressed(evt) {
            switch (evt.charCode || evt.keyCode) {
                case 38: // UP
                    if (--selectedIndex < 0)
                        selectedIndex = 0;
                case 40: // DOWN
                    if (++selectedIndex >= itemCount)
                        selectedIndex = itemCount - 1;
            return true;
        function updateSelection() {
            itemCount = 0;

            var list = document.getElementById('item_selector');
            var item = list.firstChild;
            while (item) {
                if (item.nodeName == 'LI') {
                    item.className = (itemCount == selectedIndex ? 'selected' : '');
                item = item.nextSibling;
<body onload="updateSelection()" onkeydown="return keyPressed(event);">
    <ul id="item_selector">
        <li class="selected">line1</li>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.