Saturday, April 21, 2012

pressing the space bar to select a radio button is the same as clicking upon it

I wanted to have an event fire whenever anyone altered one of the radio buttons here. I did not plan well as I wrote three seperate classes for the three seperate controls. Anyways... I wanted to make sure I covered myself for a mouse click, an arrow key click, and a space bar click. I tried to write some logic around keyCode 32 (for the space bar), but as it turns out, when you click the space bar to select a radio button, it fires the click event!

<script language="javascript" type="text/javascript">
   $(function () {
      $('.act').keypress(function (e) {
         switch (e.keyCode) {
            case 37:
               TryToCalculate();
               break;
            case 38:
               TryToCalculate();
               break;
            case 39:
               TryToCalculate();
               break;
            case 40:
               TryToCalculate();
               break;
         }
      });
      $('.act').bind('click', function () {
         TryToCalculate();
      });
      $('.one').keypress(function (e) {
         switch (e.keyCode) {
            case 37:
               TryToCalculate();
               break;
            case 38:
               TryToCalculate();
               break;
            case 39:
               TryToCalculate();
               break;
            case 40:
               TryToCalculate();
               break;
         }
      });
      $('.one').bind('click', function () {
         TryToCalculate();
      });
      $('.two').keypress(function (e) {
         switch (e.keyCode) {
            case 37:
               TryToCalculate();
               break;
            case 38:
               TryToCalculate();
               break;
            case 39:
               TryToCalculate();
               break;
            case 40:
               TryToCalculate();
               break;
         }
      });
      $('.two').bind('click', function () {
         TryToCalculate();
      });
   });
   function TryToCalculate() {
      alert("It works!");
   }
</script>

No comments:

Post a Comment