I have a span and a text input next to each other, named "one" and "two" respectively; with only one showing at a time. When you double click "one", it becomes hidden while showing and focusing on "two". When "two" is blurred, it hides and shows "one". Easy enough, the problem manifests when I try to blur "two" when the enter key is pressed. Here is what I have:
.bind({'blur keyup': (function(e){
if(e.type === 'keyup' && e.keyCode !== 10 && e.keyCode !== 13) return;
$([this,$(this).prev()]).toggle(); })
})
When the enter key is pressed, the toggle occurs but fires again when you click anywhere. So it seems blur fires, but "two" retains focus. Here is a fiddle has the two listeners separated (keyup and blur) as well as some extra to help highlight what's happening.
I tried
- adding
e.preventDefault()
- adding
return false
(before blur) --> Kills the rest of the function (as expected) - adding
return false
(after blur) --> Disables the enter key (??) or after:- keyup:
(return function(e){
--> jQuery doesn't like this syntax
- keyup:
- Performing .toggle() in a different function --> No difference, unless:
setTimeout
(even with time of 0) --> Issue goes away
- Using a different listener (key[down/press]) --> No difference
- Calling a separate function -> No difference
Question: How can I fix this? The expected behavior is that pressing the enter key yields the same action as blur.