Is it possible to focus on a I have a This will scroll to the element in question, essentially "focus"ing it. You can use tabindex The tabindex value can allow for some interesting behaviour. I wanted to suggest something like Michael Shimmin's but without hardcoding things like the element, or the CSS that is applied to it. I'm only using jQuery for add/remove class, if you don't want to use jquery, you just need a replacement for add/removeClass --Javascript --CSSfocus()
function?
window.location.hash = '#tries';
document.getElementById('tries').scrollIntoView()
works. This works better than window.location.hash
when you have fixed positioning.<div tabindex="-1" id="tries"></div>
<div id="inner" tabindex="0">
this div can now have focus and receive keyboard events
</div>
function highlight(el, durationMs) {
el = $(el);
el.addClass('highlighted');
setTimeout(function() {
el.removeClass('highlighted')
}, durationMs || 1000);
}
highlight(document.getElementById('tries'));
#tries {
border: 1px solid gray;
}
#tries.highlighted {
border: 3px solid red;
}
document.getElementById('test').onclick = function () {
document.getElementById('scripted').focus();
};
div:focus {
background-color: Aqua;
}
<div>Element X (not focusable)</div>
<div tabindex="0">Element Y (user or script focusable)</div>
<div tabindex="-1" id="scripted">Element Z (script-only focusable)</div>
<div id="test">Set Focus To Element Z</div>