How to get Currently Focused Element in JavaScript

How to get Currently Focused Element in JavaScript

In this post we will give you information about How to get Currently Focused Element in JavaScript. Hear we will give you detail about How to get Currently Focused Element in JavaScriptAnd how to use it also give you demo for it if it is necessary.

we will learn how to get a currently focused element in JavaScript.

The document.activeElement property helps us to access the element that is currently focused in the document.

It also shows body or null if there is no focused element.

Here is an example:

<div>   <input placeholder="Name"/>   <button>Hit me</button></div>

JavaScript

document.body.onclick = function(){    const element = document.activeElement.tagName    console.log(element);}

In this example, we are logging the currently selected element tag name.

Hope this code and post will helped you for implement How to get Currently Focused Element in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US