Solved

Keyboard control of drop down list

Posted on 2012-03-24
2
446 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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