1

Trigger set focus to "DIV" element on click using Angular directive

<a href="#" class="skipToContent" ng-click="showContent()" title="skip-to-main-content">Skip To Main Content</a>


<div class="getFocus" role="button" tabindex="0">
        <span>Am Focused</span> 
</div>

When I click on this the link it should shift the focus to the div

To achieve this I wrote this piece of code PSB.

I tried using scrollIntoView(); also but not sure it'll work in all the browsers and it dint work for me too.

$scope.showContent = function() {
    var x = document.querySelector('.skipToContent');
    var y = document.querySelector('.getFocus');
    y.focus();
    console.log(document.activeElement);
});
};

Note: We cannot add ids or add classes to DOM.

https://jsfiddle.net/wrajesh/wo7gkm7d/

lost_in_magento
  • 703
  • 8
  • 22
  • 1
    Also, `x.addEventListener` is the correct function you are looking for https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener – christopher clark Nov 30 '16 at 18:56
  • http://stackoverflow.com/questions/3656467/is-it-possible-to-focus-on-a-div-using-javascript-focus-function – starcorn Nov 30 '16 at 19:15

3 Answers3

2

The scrollIntoView function works in all major browsers (include chrome/firefox/ie>=8), however if you want to attach the click event to the element you should use the addEventListener function:

var x = document.querySelector('.skipToContent');
var y = document.querySelector('.getFocus');
x.addEventListener('click', function(e) {
  y.scrollIntoView()
});

Check this snippet:

var x = document.querySelector('.skipToContent');
var y = document.querySelector('.getFocus');
x.addEventListener('click', function(e) {
  e.preventDefault()
  y.scrollIntoView()
});
<a href="#" class="skipToContent" title="skip-to-main-content">Skip To Main Content</a>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="getFocus" role="button" tabindex="0">
  <span>Am Focused</span>
</div>

Note - the e.preventDefault() is there to make sure the browser ignores the default behavior of the click event on the a tag.

Dekel
  • 60,707
  • 10
  • 101
  • 129
1

Setting the focus is as easy as calling the focus() function on the target DOM element:

document.querySelector('.skipToContent').onclick = function() {
  document.querySelector('.getFocus').focus()
}
<a href="#" class="skipToContent" title="skip-to-main-content">Skip To Main Content</a>

<div class="getFocus" role="button" tabindex="0">
  <span>Am Focused</span> 
</div>
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

A better way to handle skip to main content without any JavaScript at all is this:

  1. make the skip to link an internal link pointing to the id of the wrapper div you want to skip to.
  2. make sure the targeted wrapper div has tabindex="-1" so that it can receive focus. This will help with screen readers and those using keyboard-only access.

Of course no actual scrolling will happen if the page is too short. More content, more the chance for scrolling.

<p><a href="#mainContent">Skip to main content</a></p>
<nav>
  <ul>
    <li><a href="#">nav item one</a></li>
    <li><a href="#">nav item two</a></li>
    <li><a href="#">nav item three</a></li>
  </ul>
</nav>
<div id="mainContent" tabindex="-1">
  <h1>Welcome</h1>
  <p>Here is some content</p>
</div>

I have kept the native div:focus styling so that you can see it working but you will want to style that away (only for this div. keep focus outlines for actionable elements)

haltersweb
  • 3,001
  • 2
  • 13
  • 14