{"id":58026,"date":"2023-08-23T17:37:56","date_gmt":"2023-08-23T12:07:56","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=58026"},"modified":"2024-06-10T15:37:41","modified_gmt":"2024-06-10T10:07:41","slug":"part-5-web-accessibility-fixing-checkbox-to-announce-the-checked-unchecked-state-while-using-screen-reader","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/part-5-web-accessibility-fixing-checkbox-to-announce-the-checked-unchecked-state-while-using-screen-reader\/","title":{"rendered":"Part 5 &#8211; Web Accessibility: Fixing Checkbox to announce the \u201cchecked\u201d \/ \u201cunchecked\u201d state while using screen reader"},"content":{"rendered":"<p>This is the third example for the web aria accessibility implementation where we will understand a<span style=\"font-weight: 400;\">nnouncing the <strong>checkbox label<\/strong> and <strong>checkbox checked\/unchecked state<\/strong> correctly on the <\/span><b>spacebar<\/b><span style=\"font-weight: 400;\"> key-down and <\/span><b>click<\/b><span style=\"font-weight: 400;\">. This is the working solution below on how to make this checkbox announce label and state.<\/span><\/p>\n<h2><b>\u21d2 Code SNIPPET 3 &#8211;<\/b><\/h2>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html lang=\"en\"&gt;\r\n&lt;head&gt;\r\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\r\n&lt;style&gt;\r\n* {\r\nbox-sizing: border-box;\r\n}\r\n\r\nul {\r\nlist-style-type: none;\r\npadding: 0;\r\nmargin: 0;\r\n}\r\n\r\nli {\r\nposition: relative;\r\ndisplay: inline-block;\r\n}\r\n\r\n.checkbox-custom {\r\nopacity: 0;\r\nposition: absolute;\r\n}\r\n\r\n.checkbox-custom-label {\r\nposition: relative;\r\ncursor: pointer;\r\ntext-decoration: none;\r\ndisplay: inline-block;\r\nvertical-align: middle;\r\n}\r\n\r\n.checkbox-custom-label::before {\r\ncontent: '';\r\nbackground: #fff;\r\nborder: 1px solid #ddd;\r\nwidth: 20px;\r\nheight: 20px;\r\npadding: 2px;\r\nmargin-right: 10px;\r\nmargin-bottom: 3px;\r\ndisplay: inline-block;\r\nvertical-align: middle;\r\n}\r\n\r\n.checkbox-custom-label:focus {\r\noutline: 5px auto -webkit-focus-ring-color;\r\noutline-offset: -2px;\r\n}\r\n\r\n.checkbox-custom-label[aria-checked=\"true\"]::before {\r\ncontent: '';\r\nbackground: url(.\/assets\/images\/icon-tick.svg) #000 no-repeat;\r\nbackground-position: center;\r\nbackground-size: 13px;\r\ncolor: #fff;\r\n}\r\n\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;header&gt;&lt;\/header&gt;\r\n\r\n&lt;main&gt;\r\n&lt;div class=\"container\"&gt;\r\n&lt;h2&gt;Checkbox Check and Uncheck status and label announce&lt;\/h2&gt;\r\n\r\n&lt;ul role=\"list\"&gt;\r\n&lt;li class=\"filter-checkbox\" role=\"listitem\"&gt;\r\n&lt;input tabindex=\"-1\" type=\"checkbox\" id=\"analyst-citations\" autocomplete=\"off\" class=\"checkbox-custom\"&gt;\r\n&lt;a tabindex=\"0\" for=\"analyst-citations\" onclick=\"changeCheckbox()\" onkeydown=\"changeCheckbox(event.keyCode)\" role=\"checkbox\" aria-checked=\"false\" aria-label=\"Analyst citations\" class=\"checkbox-custom-label\" id=\"checkbox-custom-label\"&gt;Analyst citations&lt;\/a&gt;\r\n&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/div&gt;\r\n&lt;\/main&gt;\r\n\r\n&lt;footer&gt;&lt;\/footer&gt;\r\n\r\n&lt;script&gt;\r\nfunction changeCheckbox(keyCode) {\r\nconst spacebarKeyCode = 32;\r\nconst item = document.getElementById(\"checkbox-custom-label\");\r\nconst checked = item.getAttribute(\"aria-checked\");\r\n\r\nif (keyCode &amp;&amp; keyCode !== spacebarKeyCode) {\r\nreturn;\r\n} else if (checked === \"true\") {\r\nitem.setAttribute(\"aria-checked\", \"false\");\r\n} else {\r\nitem.setAttribute(\"aria-checked\", \"true\");\r\n}\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>Here, on using the <strong>NVDA screen reader<\/strong> tool and by using the <strong>keyboard navigation TAB key,<\/strong> when you reach the checkbox initially, it will say as &#8211; <strong>checkbox not checked Analyst citations<\/strong>.<\/p>\n<p>Here, the screen reader reads out the state of the checkbox as well as the label of the checkbox, which here is Analyst citations.<\/p>\n<p>Once you press the <strong>spacebar key<\/strong> from the keyboard, the tick mark becomes visible inside the checkbox, and it will announce it as &#8211; <strong>checked; now,<\/strong> if you press this again, it will announce it as &#8211; <strong>not checked<\/strong>.<\/p>\n<p>You can see this same thing in action if you use the <strong>mouse<\/strong> instead of a <strong>keyboard,<\/strong> where it will announce the text on hover, and on clicking the checkbox, it will announce the same state as above.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-58025\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Part5-Img1.gif\" alt=\"checkbox gif\" width=\"600\" height=\"323\" \/><\/p>\n<p>In the next blog, you will see the last example for implementing web aria accessibility.<\/p>\n<p>Link to <b><a href=\"https:\/\/www.tothenew.com\/blog\/part-6-web-accessibility-focusing-on-visual-representation-of-navigation-links\/\">Part 6 &#8211; Web Accessibility: Focusing on Visual Representation of navigation links<\/a>.<\/b><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>This is the third example for the web aria accessibility implementation where we will understand announcing the checkbox label and checkbox checked\/unchecked state correctly on the spacebar key-down and click. This is the working solution below on how to make this checkbox announce label and state. \u21d2 Code SNIPPET 3 &#8211; &lt;!DOCTYPE html&gt; &lt;html lang=&#8221;en&#8221;&gt; [&hellip;]<\/p>\n","protected":false},"author":1622,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":217},"categories":[5868,3429],"tags":[5340,5343,5344,5341,5339],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58026"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1622"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=58026"}],"version-history":[{"count":3,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58026\/revisions"}],"predecessor-version":[{"id":58247,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/58026\/revisions\/58247"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=58026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=58026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=58026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}