{"id":101,"date":"2011-04-15T08:12:22","date_gmt":"2011-04-15T06:12:22","guid":{"rendered":"http:\/\/www.assistivetechnologygroup.org.uk\/blog\/?p=101"},"modified":"2011-04-15T08:12:22","modified_gmt":"2011-04-15T06:12:22","slug":"keyboard-traps-and-javascripts-preventdefault","status":"publish","type":"post","link":"https:\/\/www.webbie.org.uk\/blog\/keyboard-traps-and-javascripts-preventdefault\/","title":{"rendered":"Keyboard traps and Javascript&#8217;s preventDefault"},"content":{"rendered":"<p><a href=\"http:\/\/www.nomensa.com\">Nomensa<\/a> has a good article on their blog about <a href=\"http:\/\/www.w3.org\/TR\/UNDERSTANDING-WCAG20\/keyboard-operation-trapping.html\">Keyboard Traps,<\/a> or &#8220;I don&#8217;t use a mouse and the Javascript on a web page is stopping me tabbing past an item.&#8221;<\/p>\n<p>Here&#8217;s the theory. If you use Javascript events and code to override the normal keyboard operation on something like a link or button then you have to check that you can still use the page with the tab, return, space and cursor keys. If you&#8217;ve blocked this, for whatever reason, your page will no longer be usable\/accessible for keyboard users &#8211; screenreader or switch users, for example.<\/p>\n<p>The example given in the Nomensa article, however, is an odd one. It traps not clicks but keyboard activity. So it&#8217;s a link that would open a pop-up window if you pressed a key while it had focus, and uses the Javascript <code>preventDefault<\/code> statement to terminate the key activity, blocking the tab to get to a new link.<\/p>\n<p>But mouse users would not see any effect. This means it&#8217;s unlikely that this scenario will ever be coded: mouse users are generally the target audience, so the scenario is usually going to be &#8220;clicks are trapped and do something different&#8221; not &#8220;keyboard activity is trapped and does something different.&#8221;<\/p>\n<p>Here&#8217;s an example: the <a href=\"https:\/\/developer.mozilla.org\/samples\/domref\/dispatchEvent.html\">Firefox development test for <code>preventDefault<\/code>.<\/a> You can activate the blocking of normal mouse operation on the test checkbox, so you can no longer click on it to check or uncheck it. But using a keyboard you can tab past it and even check it! The development test assumes you trap the mouse click event, not keyboard events.<\/p>\n<p>This suggests that the accessibility problem with <code>preventDefault<\/code> <em>won&#8217;t <\/em>usually be the creation of keyboard traps.<\/p>\n<p>The accessibility problem <code>preventDefault<\/code> is that it will be used to create webpages <strong>where a keyboard user gets different functionality from a mouse user. <\/strong><\/p>\n<p>Most likely, this means that webpages that use <code>preventDefault<\/code> in trapped click events won&#8217;t work properly for keyboard users &#8211; for example, when you activate a link with the keyboard instead of the mouse, then instead of operating some code somewhere on the page to make a hidden piece of text visible, you trigger a navigation action.<\/p>\n<p>And look, here&#8217;s an example of exactly that problem from stackoverflow: <a href=\"http:\/\/stackoverflow.com\/questions\/265478\/preventdefault-on-an-a-tag\">preventDefault() on an &lt;a&gt; tag<\/a> Mouse users get to see text appear and disappear as they click. Keyboard users just get a click as the browser navigates to the page again.<\/p>\n<p>What to recommend? <strong>If you must use events and <code>preventDefault<\/code> then trap both mouse and keyboard events, but make sure you don&#8217;t trap tab or cursor key presses or you&#8217;ll break keyboard users. <\/strong><\/p>\n<p>But better still, <strong>don&#8217;t use Javascript to break the default activity of a webpage element.<\/strong> If you want something you click and does something use a button, not a link!<\/p>\n<ul>\n<li><a href=\"http:\/\/www.nomensa.com\/blog\/2011\/keyboard-traps\/\">Nomensa on Keyboard Traps<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Nomensa has a good article on their blog about Keyboard Traps, or &#8220;I don&#8217;t use a mouse and the Javascript on a web page is stopping me tabbing past an item.&#8221; Here&#8217;s the theory. If you use Javascript events and code to override the normal keyboard operation on something like a link or button then &hellip; <a href=\"https:\/\/www.webbie.org.uk\/blog\/keyboard-traps-and-javascripts-preventdefault\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Keyboard traps and Javascript&#8217;s preventDefault&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[23,24,25,22],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-web","tag-javascript","tag-keyboard","tag-keyboardtraps","tag-preventdefault"],"_links":{"self":[{"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":2,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webbie.org.uk\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}