简体   繁体   中英

detect currently selected input field in javascript for bookmarklet

I am trying to write a bookmarklet that fills the currently selected field in with a function of today's date. How do I find the currently tabbed to or selected input field using javascript?

To get the active element you can use

var input = document.activeElement;

if (input.tagName == "INPUT" || input.tagName == "TEXTAREA")
{
    input.value = "today's date";
}

You can use

document.activeElement

To get the current active element.

Then you can use it to input the data, like so

if (document.activeElement.nodeName === 'INPUT') {
   document.activeElement.value = new Date().getTime();
}

But your better off adding a focus event to the element

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM