Solved

Keyboard control of drop down list

Posted on 2012-03-24
2
438 Views
Last Modified: 2013-11-19
I need to control my option lists via the keyboard (for accessibility). Since I need the key navigation sequences to be the same on FF, IE, and Safari, I cannot rely on the 'accessibility' attribute.

How can I make a HTML option list 'drop down' to show its items when I tab to the option list? And then, how can I make each option list element appear as if the mouse has moved to it when I hit the tab key successively to move through the list items?

I am using JavaScript and not using any js framework like JSQuery.

Thanks in advance.

Joe
0
Comment
Question by:borgtamer
  • 2
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 37762273
Not simple to do with a standard <select>
All browsers handle focus differently. If you will change the size of the dropdown to the number of entries, it will look like it dropped down, but it will push anything after it down.
Tabbing around will not natively want to stay in the dropdown

It was decided on our site to style the dropdown - making it a widget.
It makes it behave in any way we want, but I am worried about browsers that does not see it as a select.

Accordions are also good alternatives
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37762278
For example

DEMO

<form>
    <input type="text" />
    <select id="sel1">
        <option>One</option>
        <option>Two</option>
        <option>Three</option>
        <option>Four</option>
    </select>
    <input type="text" />
</form>    

$('#sel1').bind('keydown', function(e) {
  var keyCode = e.keyCode || e.which;
  if (keyCode == 9) {
    e.preventDefault();
    if (e.shiftKey) {
      if (this.selectedIndex>0) {
      this.selectedIndex--;
      }
    }    
    else if (this.selectedIndex<this.options.length-1) {
      this.selectedIndex++;
    }
  }
})
$('#sel1').focus(function(){$(this).trigger("click")}); // does not work in Fx

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now