Skip to content Skip to sidebar Skip to footer

Blur(unfocus) An Element When Other Element Is Clicked

document.addEventListener('click', function (e) { var filter = document.getElementById('filter'); if (e.target !== filter[0]) { filter.blur(); } }); My attempt ^^ jQuery C

Solution 1:

You don't need to dereference the result of getElementById with the [0] - the result is already a single element.

The [0] is only necessary in jQuery because jQuery results are always pseudo-arrays even if there's only one matching element.

Given this function is being called for every single click on the page, consider moving the definition of filter outside of the function so that you don't have to re-evaluate it for every click (assuming that the element is static).


Post a Comment for "Blur(unfocus) An Element When Other Element Is Clicked"