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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:47:06+00:00 2026-05-29T21:47:06+00:00

I can’t seem to get the currently focused/active element as a jQuery object in

  • 0

I can’t seem to get the currently focused/active element as a jQuery object in Firefox, if it is an <input type="file" />. It works on other input types (text, password, submit, others) and on other element types (<select>, <textarea>, others).

HTML

<input type="file" />

Javascript

// Cannot find input type file elements with :focus,
// $focused.length is always 0 in Firefox (tested using FF 10.0.1)
var $focusedTest1 = $(':focus');

// This line throws for Firefox (tested using FF 10.0.1)
// Permission denied to access property 'nodeType'
// @ http://code.jquery.com/jquery-1.7.1.js:108
// in jQuery.fn.init, below "Handle $(DOMElement)"
var $focusedTest2 = $(document.activeElement);

Steps to reproduce

  1. Use Firefox.
  2. Focus the file box:
    • press tab until you reach it
    • or click in it.
  3. While focusing the file box, try to get a result from $(':focus').

See jsFiddle demonstration of getting the id of the focused element – test it with Firefox.

Does anyone have a solution for getting the focused/active element as a jQuery object that works for <input type="file" />?

The solution needs to be fully generic as the functionality is part of a plugin. I will not have control over the page the script will run on.

  • 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-05-29T21:47:07+00:00Added an answer on May 29, 2026 at 9:47 pm

    Edit: This solution has been implemented where the problem was first found, in EmulateTab. See getFocusedElement().


    Found a solution myself, after a break from coding – but it is not a very clean solution. It is basically the same solution suggested by @Neil while I first wrote this post.

    Try the updated jsFiddle version with focus listeners and try-catch logic in Firefox. It combines :focus, document.activeElement and document level focus listeners that keep track of the last “known” focused element.

    Function to find focused element

    // Comined function to get the focused element trying as long as possible.
    // Extra work done trying to avoid problems with security features around
    // <input type="file" /> in Firefox (tested using 10.0.1).
    function getFocused() {
        // Try the well-known, recommended method first.
        var $focused = $(':focus');
    
        if ($focused.size() === 0) {
            try {
                // Fall back to a fast method that might fail.
                // Known to fail for Firefox (tested using 10.0.1) with
                // Permission denied to access property 'nodeType'.
                $focused = $(document.activeElement)
            }
            catch (error1) {
                    warnToConsole("Could not use document.activeElement", document.activeElement, error1);
    
                if (lastFocusedElement !== null) {
                    try {
                        // As a last resort, use the last known focused element.
                        // Has not been tested enough to be sure it works as expected.
                        $focused = $(lastFocusedElement);
                    } catch (error3) {
                        warnToConsole("Could not use lastFocusedElement ", lastFocusedElement, error3);
                    }
                }
            }
        }
    
        return $focused;
    }
    

    Focus listeners

    // Keep a reference to the last focused element, use as a last resort.
    var lastFocusedElement = null;
    
    function focusInElement(event) {
        lastFocusedElement = event.target;
    }
    
    function focusOutElement(event) {
        lastFocusedElement = null;
    }
    
    // Start listeners.
    $(function() {
        // Start listeners that keep track of the last focused element.
        $(document).on("focusin", focusInElement);
        $(document).on("focusout", focusOutElement);
    });
    

    I don’t like this solution very much, as it is far from as clean as just a one-line $(':focus'). Other answers are welcome!

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

Sidebar

Related Questions

Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can I get a 'when to use' for these and others? <% %> <%#
Can anyone recommend a good library for generating an audio file, such as mp3,
I have a jquery bug and I've been looking for hours now, I can't
Can anyone tell me how to open Auto CAD file in HTML5? Is this
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
Can i get the source code for a WAMP stack installer somewhere? Any help
can anyone tell me if it is possible to convert a dojo charting object
can anybody confirm what are the currently allowed methods for peer-to-peer communications within the

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.