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 9171597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:11:04+00:00 2026-06-17T16:11:04+00:00

I’ve made some code to restrict tab navigation on some elements of my choice.

  • 0

I’ve made some code to restrict tab navigation on some elements of my choice. You can see the full code working here : http://jsfiddle.net/M2ELL/2/ it’s working fine.

The thing is I want to “decorate” the parent div where the focus occurs. For that matter I’ve made these event listener :

$('[data-mymodaltabindex]').blur(function () {        
   $(this).parent().removeClass('highlight');
   console.log('blur ' + $(this));
});

$('[data-mymodaltabindex]').focus(function () {
   $(this).parent().addClass('highlight');
   console.log('highlight ' + $(this));
});

These work as expected when there is only one input per parent div. On the other hand when there is two input per parent div (like the two radios in my example) the focus event occurs always before the blur event. The result is highlighting then removing the highlight on the div which is the opposite that I want.

I would like the blur event to occur before resulting in remove highlight then highlight. How can I achieve that ? Why focus event always occurs before blur event in this case ?

Full Code :

<div>
    radio 1
    <input type="radio" data-mymodaltabindex="1" />
    radio 2
    <input type="radio" data-mymodaltabindex="2" />
</div>
<div>
    text
    <input type="text" data-mymodaltabindex="3" />
</div>

$('[data-mymodaltabindex]').on('keydown', function (e) {
    if (e.keyCode == 9 || e.which == 9) {
        // define variables
        var mytabindex = null;
        var maximum = null;
        var minimum = null;
        var next = null;
        var previous = null;
        var values = new Array();
        // set mytabindex on the actual focused control
        var mytabindex = $(this).attr('data-mymodaltabindex');
        // put every visible mytabindex into array
        $('[data-mymodaltabindex]:visible').each(function () {
            values.push(parseInt($(this).attr('data-mymodaltabindex')));
        });
        //console.log(values);
        // look for maximum minimum mytabindex 
        // for maximum and minimum we filter out null values 
        // as they are interpreted as 0 with math functions
        maximum = Math.max.apply(null, values.filter(function (val) { return val !== null }));
        minimum = Math.min.apply(null, values.filter(function (val) { return val !== null }));
        // set next and previous using function
        next = getModalClosestHighValue(values, mytabindex);
        previous = getModalClosestLowValue(values, mytabindex);
        // go back to begining / end if 
        // end / begining is reached
        if (!previous) { previous = maximum; }
        if (!next) { next = minimum; }
        // check if there is shift combination
        if (e.shiftKey) {
            mytabindex = previous; // focus on the previous item
        } else {
            mytabindex = next; // focus on the next item
        }
        // focus on this element
        $('[data-mymodaltabindex=' + mytabindex + ']').focus();
        console.log('focus set to [data-mymodaltabindex=' + mytabindex + ']');
        // stop propagation
        return false;
    }
});

function getModalClosestHighValue(a, x) {
    var hi;
    for (var i = a.length; i--; ) {
        if (a[i] > x && (hi === undefined || hi > a[i])) hi = a[i];
    };
    return hi;
}

function getModalClosestLowValue(a, x) {
    var lo;
    for (var i = a.length; i--; ) {
        if (a[i] < x && (lo === undefined || lo < a[i])) lo = a[i];
    };
    return lo;
}
  • 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-17T16:11:05+00:00Added an answer on June 17, 2026 at 4:11 pm

    It turns out that it’s a bug with some jQuery versions that can be solved by using jQuery 1.9.

    Works with 1.9 : http://jsfiddle.net/7mfLb/

    Doesn’t work with 1.8.3 : http://jsfiddle.net/dHwE5/


    If you want to make it work with 1.7.2, then I propose you this workaround :

    $('[data-mymodaltabindex]').on('blur focus', function() {
       var $p = $(this).parent();
       $p.removeClass('highlight');
       setTimeout(function() {
          $('div :has(:focus)', $p.parent()).addClass('highlight');
       }, 0);
    });
    

    Demonstration

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.