{"id":2583,"date":"2020-03-09T22:43:10","date_gmt":"2020-03-10T02:43:10","guid":{"rendered":"https:\/\/agileadam.com\/?p=2583"},"modified":"2020-03-09T22:46:27","modified_gmt":"2020-03-10T02:46:27","slug":"monitoring-which-element-has-focus-in-web-browser","status":"publish","type":"post","link":"https:\/\/agileadam.com\/2020\/03\/monitoring-which-element-has-focus-in-web-browser\/","title":{"rendered":"Monitoring Which Element Has Focus in a Web Browser"},"content":{"rendered":"

While building accessibility-minded websites, it’s useful to be able to monitor which element has focus in your browser.<\/p>\n

Using a screen reader works well, but there may be an easier solution for those who aren’t used to screen readers.<\/p>\n

Chrome makes this pretty simple. See https:\/\/developers.google.com\/web\/tools\/chrome-devtools\/accessibility\/focus<\/a>.<\/p>\n

In Firefox, Safari, Chrome, and other browsers I’m able to achieve similar functionality (maybe even\u00a0better<\/em>) by running a simple snippet in the web developer tools console.<\/p>\n

New lines appear as you move through the elements with your keyboard (or mouse).<\/p>\n

You may want to turn this into a one-liner for easier pasting.<\/p>\n

var lastElement = document.activeElement;\r\nsetInterval(function() {\r\n  if (lastElement != document.activeElement) {\r\n    lastElement = document.activeElement;\r\n    console.log(document.activeElement);\r\n  }\r\n}, 100);<\/pre>\n

Output in Firefox:<\/em><\/p>\n

\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

While building accessibility-minded websites, it’s useful to be able to monitor which element has focus in your browser. Using a screen reader works well, but there may be an easier solution for those who aren’t used to screen readers. Chrome makes this pretty simple. See https:\/\/developers.google.com\/web\/tools\/chrome-devtools\/accessibility\/focus. In Firefox, Safari, Chrome, and other browsers I’m able to achieve similar functionality (maybe even\u00a0better) by running a simple snippet in the web developer tools console. New lines appear as you move through the elements with your keyboard (or mouse). You may want to turn this into a one-liner for easier pasting. var lastElement = document.activeElement; setInterval(function() { if (lastElement != document.activeElement) { lastElement = document.activeElement; console.log(document.activeElement); } }, 100); Output in Firefox:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[339,243,337,338,39],"_links":{"self":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/2583"}],"collection":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/comments?post=2583"}],"version-history":[{"count":4,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/2583\/revisions"}],"predecessor-version":[{"id":2588,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/2583\/revisions\/2588"}],"wp:attachment":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/media?parent=2583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/categories?post=2583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/tags?post=2583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}