I'm developing a select menu replacement in jquery.
First I've to make the new select menu focusable by just adding tabindex="0"
to the container.
Then, I disable focus on the original select menu and give focus to the new one.
When the new one is focused and you press the up and down arrows the options change accordingly but there's a big problem. As you press the arrows the body moves too.
I tried all these solutions so far with no luck:
$(window).unbind('scroll');
$(document).unbind('scroll');
$('body').unbind('scroll');
$(window).unbind('keydown');
$(document).unbind('keydown');
Check the code here http://pastebin.com/pVNMqyui This code is from the development version of Ideal Forms http://code.google.com/p/idealforms that I'm about to release soon, with keyboard support.
Any ideas why this is not working?
EDIT: Solved!
Found the answer on this post jquery link tag enable disable
var disableScroll = function(e){
if (e.keyCode === 40 || e.keyCode === 38) {
e.preventDefault();
return false;
}
};
// And then...
events.focus: function(){ $(window).on('keydown', disableScroll); }
events.blur: function(){ $(window).off('keydown', disableScroll); }
It works!
Its very simple.you need not even need jQuery for this.
jQuery:
javascript
Adding in style:
if you want to bind the arrow keys,try something like:
Found the answer on this post jquery link tag enable disable
In your keydown handler, for up and down keys, return false like this:
Also, make sure this return gets propagated to the browser's native onkeydown depending on how/which framework you're using.
You need to cancel the keydown event for arrow keys. Use either
e.preventDefault()
orreturn false
in your.keydown()
handler if an arrow key has been pressed.The Best way to achive the same is to set overflow of the body to hidden
After the process just do the opposite