Here's the code: http://jsfiddle.net/cPrSZ/2/

#HTML

    <p>Space changes focus to button</p>
    <button id="b1">Button1</button>

#JavaScript

    var stopProp = function(event) {
        event.stopPropagation()
        event.preventDefault()
        return false;
    };
    
    $("#b1").on("click", function(event) {
        alert("Button1 Clicked");
        return stopProp(event);
    });
    
    $("body").on("keydown", function(event) {
        if (event.which === 32) {
            $("#b1").focus();
        }
        return stopProp(event);
    });


In firefox, click on the paragraph so that the button loses focus.  Next, press 
space and you'll see that the button click gets triggered even though my code 
does not say to do that.  Why is this happening?  

If I attach the handler on `$("p").on("keydown",` instead of `body`, things 
seem to work correctly, but I'm trying to avoid having to write a handler for 
each clickable element.  If I attach a "keyup" instead of "keypress" things 
work correctly, too.  But I'm making an html5 game and I want to be able to 
react to keypress instead of keyup for responsiveness sake. 
_______________________________________________
dev-webapps mailing list
dev-webapps@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-webapps

Reply via email to