Trying to preventDefault a keydown event in Firefox on a <select>
but it does not work.
Any workaround?
For once something that even IE handles but Firefox does not!
See the code here:
http://jsfiddle.net/p8FNv/1/
<select id="select">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
<option value="7">Sunday</option>
</select>
$(document).ready(function() {
$("#select").keydown(function (event) {
event.preventDefault();
event.stopPropagation();
});
});
The following example demonstrates how invalid text input can be stopped from reaching the input field with preventDefault() . Nowadays, you should usually use native HTML form validation instead.
The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link. Parameters: It does not accept any parameter.
stopPropagation prevents further propagation of the current event in the capturing and bubbling phases. preventDefault prevents the default action the browser makes on that event.
As you probably might have already worked out based on the simple explanation above: the opposite of event. preventDefault() is nothing. You just don't prevent the event, by default the browser will allow the event if you are not preventing it.
This is not much elegant but works:
$("select").keydown(function(e){
e.preventDefault();
var elem = this;
elem.disabled=true;
setTimeout(function() { elem.disabled = false; }, 0);
});
A greeting.
This seems to be working for me on Firefox 19. I am assuming your intention is to prevent the select value from changing via keyboard input when a user types in a value.
$(document).ready(function() {
var valueToKeep;
$("#select").keypress(function (event) {
$(this).val(valueToKeep);
});
$("#select").keydown(function (event) {
event.preventDefault();
event.stopPropagation();
valueToKeep = $(this).val();
});
});
JSFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With