I have a selector that selects multiple elements. I want to check if at least one of these elements has the focus.
By experimenting, I noticed that .is(":focus") and .has(":focus") didn't work when dealing with multiple elements, see below:
console.log( $('.text-block').is(":focus") ); // prints false
console.log( $('.text-block').has(":focus") ); // prints empty object array
console.log( $('.text-block').filter(":focus") ); // prints empty object array
console.log( $('.text-block:focus') ); // prints one element (works!)
as :focus selector seems the only way to go, my question is: how to do if my elements ($('.text-block')) are in a variable (like $myElements) ?
(Second question would be to understand why only :focus selector works for me.)
document.activeElement.focus()or are you checking in another function?$('.text-block').is(document.activeElement).