-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arrow keys do not change value of select #3472
Comments
+1000 |
yes please |
Is there a solution for this? |
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key === KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if ((key === KEYS.SPACE && evt.ctrlKey)) {
self.trigger('results:toggle');
evt.preventDefault();
} else if (key === KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key === KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault();
}
} else {
if (key === KEYS.ENTER || key === KEYS.SPACE ||
((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) {
self.open();
evt.preventDefault();
}
if (key === KEYS.DOWN) {
if (undefined != this.$element.find('option:selected').next().val()) {
this.$element.val(this.$element.find('option:selected').next().val());
this.$element.trigger('change');
}
evt.preventDefault();
}
if (key === KEYS.UP) {
if (undefined != this.$element.find('option:selected').prev().val()) {
this.$element.val(this.$element.find('option:selected').prev().val());
this.$element.trigger('change');
}
evt.preventDefault();
}
}
}); |
@uzlov what is this? where do you put it? |
@uzlov seems good. |
@rain01 |
+1 |
@uzlov could you submit a PR? |
I believe the change @uzlov suggests have to be made in line An easy change that improves the usability and accessibility of the plugin. In my case my boss wouldn't allow me to use it unless this feature was provided. Still need to fix the autofocus, but hopefully I'll manage to do it. |
Or if you prefer, line 5150 of the unminified distributed file. |
+1 |
1 similar comment
+1 |
For sure, +1. I'm surprised this is an issue still ! |
+1 |
1 similar comment
👍 |
@uzlov 's code is working fine for me. Any change that this will be included in the next version (4.0.2?) of select2? |
I've been using @uzlov code, rather than modifying the code I just go: s2Obj = $(element).data('select2');
delete s2Obj.listeners.keypress;
s2Obj.on('keypress', function (evt) {
//his code here
});
|
Note: in @uzlov code, the KEYS.DOWN and KEYS.UP items should be changed to only select the enabled options: if (key === KEYS.DOWN) {
var val = s2Obj.$element.find('option:selected').nextAll(":enabled").first().val();
if (undefined !== val) {
s2Obj.$element.val(val);
s2Obj.$element.trigger('change');
}
evt.preventDefault();
}
if (key === KEYS.UP) {
var val = s2Obj.$element.find('option:selected').prevAll(":enabled").first().val();
if (undefined !== val) {
s2Obj.$element.val(val);
s2Obj.$element.trigger('change');
}
evt.preventDefault();
} |
+1 |
+1 |
It's probably best to not modify the library contents ad-hoc (and especially difficult if you're serving from a CDN). You can implement uzlov's suggestion (and phazei's update) by attaching a listener to the object after the library has already loaded. You can listen for key strokes on the // listen for keyups in both input widget AND dropdown
$("body").on('keyup', ".select2,.select2-dropdown", function (e) {
var KEYS = { UP: 38, DOWN: 40 };
var $sel2 = $(this).closest(".select2");
// if we can't find it by traveersing the dom, search page for an open container
// ASSUMES only one open at a time -
// don't really know how to cross walk from dropdown back to coresponding element
if ($sel2.length == 0) {
$sel2 = $(".select2.select2-container--open");
}
// make sure we found the <select> el
var $sel = $sel2.data("element")
if ($sel.length) {
var newValue
if (e.keyCode === KEYS.DOWN && !e.altKey) {
newValue = $sel.find('option:selected').nextAll(":enabled").first().val();
} else if (e.keyCode === KEYS.UP) {
newValue = $sel.find('option:selected').prevAll(":enabled").first().val();
}
// if we got a value, set it and update
if (newValue != undefined) {
$sel.val(newValue);
$sel.trigger('change');
}
}
}); Demo in jsFiddle |
@KyleMit this is a nice solution. I'm also of the "I don't want to to modify core lib code" camp, so until #4293 is merged this workaround will suffice. The only thing I've noticed is there's no |
@kfrz What worked for me was changing over to a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
+1 |
Any update on this? |
Any update on this? |
Any news on this issue ? Have not been able to find a solution with 4.0.13 release. |
Here is a siple solution that runs with select2 4.0.13
The jsfiddle is available from https://jsfiddle.net/PBrockmann/ktcnr6o7/ |
@PBrockmann That works great. The only issues I had were:
|
Thank you for the feedback but I do not have the same behaviour with google-chrome. Could you precise what browser is concerned ? |
According to #3279 the feature of opening the select2 search dropdown is not implemented in the newer version according to technical difficulties, but also because it does not reflect the default behaviour of a native select.
Though, following this native select behaviour, the user should be able to browse through the dropdown items by using the arrow keys on the keyboard. This does not seem to work for the select2 4.0 implementation. Is there a reason for this?
The text was updated successfully, but these errors were encountered: