Suppose you have the following structure in your HTML and some elements inside the content div have various key (press, up, down) events:
<html>
<head></head>
<body>
<div id="content">All the content</div>
</body>
</html>
You add an absolute positioned div by jQuery:
$('body').append('<div id="lightbox">etc.</div>');
How can you temporarily (during the existance of the lightbox) prevent all keyboard events from being caught by or bubbled to only $('#content') and its children, so that the bubbling order when the lightbox exists for the above structure will be
$('#lightbox *')$('#lightbox')$('body')$('html')
whereas it will be
$('#content *')$('#content')$('body')$('html')
when the lightbox is gone?
EDIT: Here’s some extra info.
-
I can’t make all the form elements disabled or readonly (this applies to limited elements anyway) because lightbox may occasionally exist on top of many form elements that have already assigned unique key events.
-
Also, I want key events to affect window + (lightbox, if any, otherwise content) so I can’t just stop all event propagation directly
-
The orders above can be 4-1 instead of 1-4, it doesn’t matter. What matters is to bypass the contents of this specific element (lightbox or content acc. to the situation) in key events.
I could not find any reliable solution so I chose to write my own function(s). Although I’m not much happy messing up with it, I had to control the behavior of the TAB key and create global keyup/keydown functions.
For those interested I found some resources in SO, here, here and here.
Note that I’m assigning a CSS class named “disabled” to the anchors in my script so you can consider those that you’ll see in my code like
disabled="disabled"attribute of applicable elements. Also, I could not find a way to make it focus out from the html content into the selectable UI elements of the browser but that’s probably the last concern.I hope this saves some time for others with a similar issue.