6

I have jQuery code which makes an array of focusable elements and binds .keydown for the left and right arrows to tab through them. In Chrome, IE, and Safari beginning with preventDefault() or ending with a return false (which technically I don't want to use because I have no need to stopPropagation()) prevents the default event of the arrows, but in Firefox it does not.

How can I prevent the default action in Firefox as well?

Here is the code, which works as expected, except in Firefox where the default event fires in addition to my callback.

$(function () {
    var focusables = $(":focusable");
    focusables.eq(0).focus();
    focusables.eq(0).select();
    focusables.each(function () {
        $(this).keydown(function (e) {
            if (e.which == '37') { // left-arrow
                e.preventDefault();
                var current = focusables.index(this),
                    next = focusables.eq(current - 1).length ? focusables.eq(current - 1) : focusables.eq(0);
                next.focus();
                next.select();
            }
            if (e.which == '39') { // right-arrow
                e.preventDefault();
                var current = focusables.index(this),
                    next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
                next.focus();
                next.select();
            }
        });
    });
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brandon
  • 779
  • 5
  • 9
  • 28

2 Answers2

8

The keypress event is the one which would need to be canceled, but Firefox ignores preventDefault() in this scenario. So the solution is to blur the current dropdown, let the keypress event fire on the document and set the focus to the new dropdown via timeout.

var focusables = $(":focusable");
focusables.eq(0).focus().select();
focusables.each(function () {
    $(this).keydown(function (e) {
        if (e.which == '37') { // left-arrow
            e.preventDefault();
            var current = focusables.index(this),
                next = focusables.eq(current - 1).length ? focusables.eq(current - 1) : focusables.eq(0);
            this.blur();
            setTimeout(function() { next.focus().select(); }, 50);
        }
        if (e.which == '39') { // right-arrow
            e.preventDefault();
            var current = focusables.index(this),
                next = focusables.eq(current + 1).length ? focusables.eq(current + 1) : focusables.eq(0);
            this.blur();
            setTimeout(function() { next.focus().select(); }, 50);
        }
    });
});

Demo at http://jsfiddle.net/roberkules/3vA53/

roberkules
  • 6,557
  • 2
  • 44
  • 52
  • This is not working for me. Perhaps the reason that this is working for your code is because you are using input text boxes, where as I am primarily navigating drop-down boxes. When I navigate to the next drop-down box, the value of the newly focused-on list changes to the next value. – Brandon May 24 '11 at 14:52
  • I see, once jsfiddle is back online I'll have a look. – roberkules May 24 '11 at 15:00
  • You're welcome. Unfortunately I can't see a real solution, just this workaround. – roberkules May 24 '11 at 17:30
1

Have you tried this?

$(selector).click(function(event) {
   event.preventDefault();
});
Conner
  • 30,144
  • 8
  • 52
  • 73
Traveling_Monk
  • 778
  • 2
  • 11
  • 28