Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:10:41+00:00 2026-06-11T13:10:41+00:00

Suppose you have the following structure in your HTML and some elements inside the

  • 0

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

  1. $('#lightbox *')
  2. $('#lightbox')
  3. $('body')
  4. $('html')

whereas it will be

  1. $('#content *')
  2. $('#content')
  3. $('body')
  4. $('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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:10:43+00:00Added an answer on June 11, 2026 at 1:10 pm

    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.

    // lightbox keyboard navigation
    function getFocusList() {
        return $('body,#lightbox *:visible').filter(function(idx,elm) {
            if ($(elm).is('body')) return true;
            if (/^hidden$/i.test($(elm).css('visibility'))) return false;
            return (
                (/^a(rea)?$/i.test(elm.nodeName) && $(elm).hasAttr('href') && !$(elm).is('.disabled')) ||
                (/^(input|textarea|button|select)$/i.test(elm.nodeName) && !$(elm).prop('disabled')) ||
                (/^\d+$/.test($(elm).prop('tabindex')) && !$(elm).prop('disabled') && !$(elm).is('.disabled'))
            )
        });
    }
    function globalKeyDown(e) {
        var
            LB          = !!$('#lightbox').length,
            isTAB       = e.which == 9,
            isSHIFT     = e.which == 16,
            SHIFT_ON    = LB && typeof $('#lightbox').data('SHIFT_ON') != 'undefined'
        if (isSHIFT && LB) $('#lightbox').data('SHIFT_ON',true);
        if (!isTAB || !LB) return;
        var focusList = getFocusList(), nextIndex;
        var curIndex = $.inArray($(this).get(0),focusList);
        if (SHIFT_ON) nextIndex = --curIndex == 0 ? focusList.length - 1 : curIndex;
        else nextIndex = ++curIndex == focusList.length ? (focusList.length > 1 ? 1 : 0) : curIndex;
        var next = $(focusList[nextIndex]);
        e.preventDefault();
        e.stopImmediatePropagation();
        next.focus();
    }
    function globalKeyUp(e) {
        var
            LB          = !!$('#lightbox').length,
            isSHIFT     = e.which == 16,
            SHIFT_ON    = LB && typeof $('#lightbox').data('SHIFT_ON') != 'undefined'
        if (isSHIFT && SHIFT_ON) $('#lightbox').removeData('SHIFT_ON')
    }
    
    $.fn.extend({
        hasAttr:function(attrStr) {
            var res = true;
            $(this).each(function() {
                if (typeof $(this).attr(attrStr) == 'undefined') res = false;
            });
            return res;
        }
    });
    $(document).ready(function() {
        $('body')
        .on('focus','#lightbox a',function() {$(this).addClass('hover')})
        .on('blur','#lightbox a',function() {$(this).removeClass('hover')})
        .on('keydown','*',globalKeyDown).on('keyup','*',globalKeyUp)
        .on('keydown',globalKeyDown).on('keyup',globalKeyUp)
        // remaining jQuery code...
    });
    

    I hope this saves some time for others with a similar issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following directory structure in Vim's NERDTree: /some/folder/ |-apples.txt |-bananas.txt |-
Suppose I have the following html: var testString = ' <span id='id'> <div> <table>
Suppose I have the following directory structure: lib\ --__init__.py --foo.py --bar.py Inside foo and
Let's suppose to have the following html structure (1). From $('.child') element I can
Suppose I have the following HTML code: <div id=container style=width: 960px> <div id=help_panel style=width:
Suppose I have the following XML structure: <root> <item> <item1>some text is <b>here</b></item1> <item2>more
Suppose I have following frame structure. <frameset rows=20%,60%, *> <frame name=theFrame id=theFrame src=test1.html >
Suppose we have a page with the following structure: <li id=A> <span class=some class>some
suppose you have the following structure: #include <windows.h> // BOOL is here. #include <stdio.h>
Suppose I have the following (desired) folder structure: *CommonProject *Project#1 ----> CommonProject(link) *Project#2 ---->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.