Cannot use jQuery consistently cross-browser on focused element if the element is <input type="file" />.
Using $(':focus') gives no results in Firefox (tested using 10.0.1), but seems to work in other browsers.
$(document.activeElement) throws in Firefox (tested using 10.0.1) with "Permission denied to access property 'nodeType'."
HTML
<html>
<body>
<h1>Problem</h1>
<p>Cannot use jQuery consistently cross-browser on focused element if the element is <input type="file" />.</p>
<h1>A file input</h1>
<input id="my-input-type-file" type="file" />
<h1>Other fields for testing</h1>
<input id="my-input-type-text" type="text" value="Focus to log id" />
<input id="my-input-type-checkbox" type="checkbox" value="Focus to log id" />
<select id="my-select-dropdown"><option>Focus to log id</option></select>
<h1>Logging id of focused element</h1>
<ol id="log"></ol>
</body>
</html>
CSS
h1
{
font-weight: bold;
margin-top: 1em;
}
ol
{
padding-left: 3em;
list-style-type: decimal;
}
JavaScript
function logFocusedElement() {
var $focused;
// Cannot find input type file elements with :focus,
// $focused.length is always 0 in Firefox (tested using FF 10.0.1)
$focused = $(':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)"
//$focused = $(document.activeElement);
// Selecting file input by id/type/:file works, but it's not possible
// to know id/type/:file in a generic focus-dependent function
//$focused = $('#my-input-type-file');
//$focused = $('input[type=file]');
//$focused = $(':file');
logElement($focused);
}
function logElement($element) {
var id = $element.attr('id');
// Log to console
try {
console.log(id, $element.length, $element);
} catch (e) {}
// Log to page for fast feedback
var $log = $('#log');
$('<li />', {
html: (id || "")
}).prependTo($log);
// Trim log list
$log.children('li:gt(99)').remove();
}
var interval = 500;
var intervalId = setInterval(logFocusedElement, interval);