Prevent default event for jQuery keydown in Firefox
Solution 1
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/
Solution 2
Have you tried this?
$(selector).click(function(event) {
event.preventDefault();
});
Brandon
I have a Bachelor's of Science in Computer Science and Engineering, a Bachelor's of Science in Computer Mathematics, and a Master's of Science in Game Design. Some experience with: ASP Classic, ASP.NET, Assembly, AngularJS, C, C++, C#, CSS3, Express, HTML5, JavaScript, jQuery, MongoDB, MVC, MVVM, MySQL, NodeJs, Perl, PHP, Razor, SQL Server, VBScript, Visual Basic, WPF I'm trying to focus more on game development. Some experience with: UDK, Unity, Vision, AndEngine, OpenGL
Updated on July 31, 2022Comments
-
Brandon almost 2 years
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 tostopPropagation()
) 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(); } }); }); });
-
Brandon almost 13 yearsThis 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.
-
talltolerablehuman almost 13 yearsI see, once jsfiddle is back online I'll have a look.
-
talltolerablehuman almost 13 yearsYou're welcome. Unfortunately I can't see a real solution, just this workaround.
-
Rudey almost 9 years@Brandon see this for Firefox support.