is it possible to check if an element is focused?
i tryed:
<a href="#"></a>
$('a').is(':focused');
but it won't work.
Also i would like to understand if is possible to get the last focused document element attr class:
var last_focused_class = document.lastFocusedElement.class;
for example
is it possible to check if an element is focused?
i tryed:
<a href="#"></a>
$('a').is(':focused');
but it won't work.
Also i would like to understand if is possible to get the last focused document element attr class:
var last_focused_class = document.lastFocusedElement.class;
for example
Share Improve this question asked May 16, 2013 at 17:04 Filippo orettiFilippo oretti 49.8k96 gold badges229 silver badges351 bronze badges 3-
have you tried
is(':focus')
(docs). – David Thomas Commented May 16, 2013 at 17:07 -
Try
$('a').is(":focus");
instead of$('a').is(':focused');
– palaѕн Commented May 16, 2013 at 17:07 - oh god, this works sorry i reformulating my question so – Filippo oretti Commented May 16, 2013 at 17:08
2 Answers
Reset to default 14You were close, to test for focus:
$('a').is(':focus');
Not sure about how to get the last focused element, you may have to design a hack for it...something like:
var lastFocusedClass;
$(document).on('focus', '*', function() {
lastFocusedClass = $(this).attr("class");
});
In my case this works... $("*:focus")
You can get id
,name
,value
etc.And check if this is the element you are looking for.
var checkValue = $("*:focus").attr("...");