12

Newbie- I want to do 2 things with these checkboxes:

  1. Use TAB key to tab through options, this part works
  2. As I TAB through options, I want to hit ENTER key to select that check box, this part is NOT working

Below is sample code. I am using the checkboxes as a group.

Does any one have any suggestions?

<!doctype html>
    <head>
        <title>test Radio buttons checkbox</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('input:checkbox[name=Colors]').keypress(function(event) {
                    var keycode = (event.keyCode ? event.keyCode : event.which);
                    if (keycode == 13) {
                        Checkbox_to_RadioButton(this);
                        alert("Enter key was pressed");
                    }   
                    event.stopPropagation();
                });

                $('input:checkbox[name=Colors]').click(function(){
                    Checkbox_to_RadioButton(this);
                });
            });

            function Checkbox_to_RadioButton(box){
                $('input:checkbox[name=' + box.name + ']').each(function(){
                    if (this != box)
                        $(this).attr('checked', false);
                });
            }
        </script>
    </head>

    <body>
        <h1>test Radio buttons checkbox</h1>
        <form name="form1">
            <input type="text" id="dname" name="dname"><br>
            <input type="checkbox" id="Colors" name="Colors" value="Red" />Red<br />
            <input type="checkbox"  id="Colors" name="Colors" value="Blue" />Blue<br />
            <input type="checkbox" id="Colors"  name="Colors" value="Green" />Green<br     />
            <input type="checkbox" id="Colors"  name="Colors" value="Yellow"         />Yellow<br /> 
            <br>
        </form>
    </body>
</html>
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
Steve42
  • 211
  • 2
  • 5
  • 10

5 Answers5

15

I found the recommended solution to be too bloated. this works better

$('input:checkbox').keypress(function(e){
    if((e.keyCode ? e.keyCode : e.which) == 13){
        $(this).trigger('click');
    }
});
r3wt
  • 4,642
  • 2
  • 33
  • 55
6

r3wt's approach works beautifully, but I noticed on my forms that pressing Enter would also submit the form or otherwise do something else unwanted. Adding e.preventDefault(); prevents the default browser action when pressing enter on the checkbox.

$('input:checkbox').keypress(function(e){
    e.preventDefault();
    if((e.keyCode ? e.keyCode : e.which) == 13){
        $(this).trigger('click');
    }
});
rogi
  • 61
  • 1
  • 1
5

Try this code

<!doctype html>
<html>
  <head>
    <title>test Radio buttons checkbox</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('input:checkbox[name=Colors]').keypress(function(event) {
                var keycode = (event.keyCode ? event.keyCode : event.which);
                if (keycode == 13) {
                    Checkbox_to_RadioButton(this,"enter");
                }   
                event.stopPropagation();
            });

            $('input:checkbox[name=Colors]').click(function(){
                Checkbox_to_RadioButton(this,"click");
            });
        });

        function Checkbox_to_RadioButton(box,myEvent){
            if(myEvent == "enter")
            {
                var $box = $(box);
                if($box.attr('checked'))
                    $box.attr('checked',false);
                else
                    $box.attr('checked',true);
            }
            $('input:checkbox[name=' + box.name + ']').each(function(){
                if (this != box)
                    $(this).attr('checked', false);
            });
        }
    </script>
</head>

<body>
    <h1>test Radio buttons checkbox</h1>
    <form name="form1">
        <input type="text" id="dname" name="dname"><br>
        <input type="checkbox" id="Colors" name="Colors" value="Red" />Red<br />
        <input type="checkbox"  id="Colors" name="Colors" value="Blue" />Blue<br />
        <input type="checkbox" id="Colors"  name="Colors" value="Green" />Green<br     />
        <input type="checkbox" id="Colors"  name="Colors" value="Yellow"         />Yellow<br /> 
        <br>
    </form>
  </body>
</html>
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
yoku2010
  • 600
  • 1
  • 4
  • 14
0

This works for me.

$('input:checkbox').keypress(function(e){
    if((e.keyCode ? e.keyCode : e.which) == 13){
        $(this).trigger('click');
    }
});
Pri Nce
  • 576
  • 6
  • 18
-1

First of all, you're missing a bracket before the form tag.

Second, you forgot to actually activate the checkbox on keypress:

if (keycode == 13) {
    $(this).attr('checked', checked); // << this line!
    Checkbox_to_RadioButton(this);
    alert("Enter key was pressed");
}   

Working Fiddle here.

vzwick
  • 11,008
  • 5
  • 43
  • 63