views:

401

answers:

2

I have a page full of radio buttons that I'd like to be able to tab through and then select options based on keyboard input. I'm hoping to get the keyboard input to work on radiobuttons in a similar way to how it works on dropdowns (typing the letter 'G' will automatically select the first option that starts with a 'G')

Is there a jquery plugin out there that can help me with this or do I have to put together the functionality from scratch. Unfortunately, as per customer request, the options need to stay in radiobutton (not dropdown) format and the built-in tab/arrow keys/spacebar functionality isn't quite enough.

Thanks

+1  A: 

Using HTML rather than jQuery, browsers have a built-in mechanism to select form options for keyboard input + a modifier key using the 'accesskey' attribute. I'm pretty sure jQuery could interact with this method if necessary.

<p>To select with a keyboard, click "alt+a" or "alt+b"*</p>
<form name="myForm" method="post">
    <input type="radio" accesskey="a" value="a" name="myRadio" /> Value A
    <input type="radio" accesskey="b" value="b" name="myRadio" /> Value B
    <input type="submit" name="submit" value="submit" />
</form>

*See for more explanation: http://www.webaim.org/techniques/keyboard/accesskey.php

Dan Sorensen
I can see potential with this, but I'm looking more for something that will look at the text and deal with access key automatically. Another feature of drop downs that I didn't mention above is that hitting 'G' twice will move to the second response that starts with 'G'. Another sticker for me is that it looks like the different browsers use different keystroke combinations.
Mercurybullet
Are you going to assume the first letter of the radio button text? What if there are duplicates?
Dan Sorensen
Yup, going to assume the first letter of the radio button text. Like I said, I'd love to have something that replicates the drop down menu style exactly if possible, but not enough to create it from scratch. Minimum requirement would be to loop through all options that start with the letter being pressed. If possible, it would be great to support quick bursts of the first few letters.
Mercurybullet
In that case, it would be helpful for you to post a snippit of your radio button HTML. I'm afraid you may run into issues duplicating the dropdown select input since many keys are already mapped to a browser function without a keyboard modifier. It works when the select input when it is in focus. I'm not sure if it's possible to focus on a group of radio buttons in the same way.
Dan Sorensen
+1  A: 

Here is some examples of code you might use. Add your own Case parts as needed, put your selectors in to match your structure.

One has passed the current selection, the other has not. Use as you wish and make specific to your need.

 /* handle special key press */
    function checkKey(e)
    {
        var shouldBubble = true;
        switch (e.keyCode)
        {
            // user pressed the Tab                                           
            case 9:
                {
                    $(".pickMyClass").toggleClass("pickSelectVisible");
                    $("#someOtherClass").toggleClass('pickHighlight');
                    shouldBubble = false;
                    break;
                };
                // user pressed the Enter    
            case 13:
                {
                    $(".pickMyClass").toggleClass("pickSelectVisible");
                    $('.pickEntryArea').trigger('change'); /* we made changes so trigger it */
                    $("#someOtherClass").toggleClass('pickHighlight');
                    break;
                };
                // user pressed the ESC
            case 27:
                {
                    $(".pickMyClass").toggleClass("pickSelectVisible");
                    $("#someOtherClass").toggleClass('pickHighlight');
                    break;
                };
        };
        /* this propogates the jQuery event if true */
        return shouldBubble;
    };
    /* handle special key press */
    function checkFieldKey(e, me)
    {
        var shouldBubble = true;
        switch (e.keyCode)
        {

            // user pressed the Enter           
            case 13:
                {
                    $(me).blur();
                    $("#someSpecialSelect").focus();
                    shouldBubble = false;
                    break;
                };
        };
        /* this propogates the jQuery event if true */
        return shouldBubble;
    };
    /* user pressed special keys while in Select */
    $(".someSelect").keydown(function(e)
    {
        return checkKey(e);
    });
    /* user pressed special keys while in Select */
    $(".somethingOther").keydown(function(e)
    {
        return checkFieldKey(e, $(this));
    });
Mark Schultheiss
This framework looks great and I might end up using it if I need to build something from scratch, but unfortunately I'm looking for something that looks at the text and checks for keys automatically rather than having to define how each key should act. Basically looking for something I can drop into multiple places without having to change any code and it will allow radio buttons to react to keyboard input in the same way dropdowns do.
Mercurybullet
You could put a Case for characters in place, then have a generic process to manage the reaction to the key entries - and create a simple plug-in from that...just a thought. It should not be that difficult. I have not looked for a "radio character" type plug-in.
Mark Schultheiss
Really, it sounds like you want to process key strokes like the Autocomplete plug-in but have it act upon the text in the radio buttons - so it should be possible to extract the key stroke processing from Autocomplete and create a new plugin from there, or use what I have given as a baseline start point.
Mark Schultheiss
Since there doesn't seem to be anything prebuilt out there, I'll accept your answer. Thanks for the help, this framework should really streamline the process of building a quick plugin that checks keyboard input versus the available radio button options.
Mercurybullet