{"id":10471,"date":"2025-10-20T09:32:24","date_gmt":"2025-10-20T09:32:23","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10471"},"modified":"2025-10-20T09:32:24","modified_gmt":"2025-10-20T09:32:23","slug":"building-accessible-components-with-vanilla-javascript","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/building-accessible-components-with-vanilla-javascript\/","title":{"rendered":"Building Accessible Components with Vanilla JavaScript"},"content":{"rendered":"<h1>Building Accessible Components with Vanilla JavaScript<\/h1>\n<p>Creating accessible web applications is not just a best practice; it\u2019s a necessity for reaching the entire audience. In this article, we will delve into building accessible components using Vanilla JavaScript. By adhering to the principles of accessibility, you make your applications usable for people with disabilities, ensuring a better experience for all users.<\/p>\n<h2>Understanding Web Accessibility<\/h2>\n<p>Web accessibility means that websites, tools, and technologies are designed and developed so that people with disabilities can use them. More than 1 billion people worldwide experience some form of disability, making accessibility an essential aspect of web development.<\/p>\n<p>There are multiple guidelines and standards developers can follow, such as the <strong>Web Content Accessibility Guidelines (WCAG)<\/strong>. These guidelines offer a practical approach to making web content more accessible to people with disabilities, focusing on four core principles: Perceivable, Operable, Understandable, and Robust (POUR).<\/p>\n<h2>Why Use Vanilla JavaScript?<\/h2>\n<p>While libraries and frameworks like React, Angular, and Vue offer powerful tools for developing user interfaces, sometimes, using Vanilla JavaScript is a more accessible option for developers, especially for basic components. Using Vanilla JavaScript can also reduce complexity, as it avoids the overhead associated with frameworks. Building directly in JavaScript allows for a deeper understanding of accessible practices.<\/p>\n<h2>Key Accessibility Features to Consider<\/h2>\n<ul>\n<li><strong>Keyboard Navigation:<\/strong> Ensure users can navigate your application using their keyboard.<\/li>\n<li><strong>Semantic HTML:<\/strong> Use semantic tags appropriately to convey meaning.<\/li>\n<li><strong>ARIA Roles:<\/strong> Use ARIA (Accessible Rich Internet Applications) to enhance your components.<\/li>\n<li><strong>Focusable Elements:<\/strong> Manage focus states effectively for dynamic updates.<\/li>\n<li><strong>Color Contrast:<\/strong> Ensure sufficient contrast between text and background for readability.<\/li>\n<\/ul>\n<h2>Creating an Accessible Button Component<\/h2>\n<p>Let\u2019s begin by creating a simple button component from scratch using Vanilla JavaScript. We will focus on making sure it is accessible for all users.<\/p>\n<h3>HTML Structure<\/h3>\n<p>Start with a simple button structure:<\/p>\n<pre><code>&lt;button id=\"myAccessibleButton\" aria-label=\"Submit Form\"&gt;Submit&lt;\/button&gt;<\/code><\/pre>\n<p>This button has an <strong>aria-label<\/strong> to provide context for screen readers, which is a critical step in creating accessible components.<\/p>\n<h3>Styling the Button<\/h3>\n<p>You can easily style the button in your CSS:<\/p>\n<pre><code>button {\n    background-color: #007BFF;\n    color: white;\n    padding: 10px 20px;\n    border: none;\n    border-radius: 5px;\n    cursor: pointer;\n    font-size: 16px;\n}\n\nbutton:focus {\n    outline: 2px solid #FFC107; \/* Adding focus outline for better visibility *\/\n}<\/code><\/pre>\n<p>Using a visible focus outline is essential for keyboard users to know where they are on the page.<\/p>\n<h3>JavaScript Functionality<\/h3>\n<p>The button needs an event listener to handle the click event:<\/p>\n<pre><code>document.getElementById('myAccessibleButton').addEventListener('click', function() {\n    alert('Form Submitted!');\n});<\/code><\/pre>\n<p>This simple JavaScript function provides an alert when the button is clicked, demonstrating the button&#8217;s functionality.<\/p>\n<h2>Accessible Form Component<\/h2>\n<p>Forms are fundamental UI components, but they can present accessibility challenges. Here, we\u2019ll create an accessible form with a text input and a submit button.<\/p>\n<h3>HTML Structure for the Form<\/h3>\n<pre><code>&lt;form id=\"myForm\" aria-labelledby=\"formTitle\"&gt;\n    &lt;h2 id=\"formTitle\"&gt;Contact Us&lt;\/h2&gt;\n    &lt;label for=\"name\"&gt;Name:&lt;\/label&gt;\n    &lt;input type=\"text\" id=\"name\" required aria-required=\"true\"&gt;\n    \n    &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;\n    &lt;input type=\"email\" id=\"email\" required aria-required=\"true\"&gt;\n    \n    &lt;button type=\"submit\" id=\"myFormButton\"&gt;Send&lt;\/button&gt;\n&lt;\/form&gt;<\/code><\/pre>\n<h3>Applying JavaScript for Form Submission<\/h3>\n<p>Manage form submission while keeping accessibility in mind:<\/p>\n<pre><code>document.getElementById('myForm').addEventListener('submit', function(event) {\n    event.preventDefault();\n\n    const name = document.getElementById('name').value;\n    const email = document.getElementById('email').value;\n\n    alert(`Thank you, ${name}. Your submission for ${email} was successful!`);\n});<\/code><\/pre>\n<p>This function captures the input values and displays a message while also preventing the default form submission to ensure screen readers can process the alert message easily.<\/p>\n<h2>Implementing ARIA Attributes<\/h2>\n<p>ARIA attributes can add extra context to components, especially complex ones. For instance, when creating a collapsible menu, ARIA roles can help:<\/p>\n<pre><code>&lt;button id=\"menuToggle\" aria-expanded=\"false\" aria-controls=\"menu\"&gt;Toggle Menu&lt;\/button&gt;\n&lt;ul id=\"menu\" role=\"menu\" aria-hidden=\"true\"&gt;\n    &lt;li role=\"menuitem\"&gt;Home&lt;\/li&gt;\n    &lt;li role=\"menuitem\"&gt;About&lt;\/li&gt;\n    &lt;li role=\"menuitem\"&gt;Contact&lt;\/li&gt;\n&lt;\/ul&gt;<\/code><\/pre>\n<p>In the JavaScript, update the ARIA attributes:<\/p>\n<pre><code>document.getElementById('menuToggle').addEventListener('click', function() {\n    const menu = document.getElementById('menu');\n    const isExpanded = this.getAttribute('aria-expanded') === 'true';\n\n    this.setAttribute('aria-expanded', !isExpanded);\n    menu.setAttribute('aria-hidden', isExpanded);\n});<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Creating accessible components using Vanilla JavaScript is not just possible; it is imperative. By following the principles of accessibility and employing semantic HTML, ARIA roles, and effective keyboard navigation, you can ensure your web applications are usable by everyone, including people with disabilities.<\/p>\n<p>These practices not only enhance user experience but also foster inclusivity, which can lead to increased engagement and a broader audience reach. The web should be a place where everyone can interact, share, and thrive. Keep creating with accessibility in mind, and your efforts will pave the way for a more inclusive digital world.<\/p>\n<h2>Further Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.w3.org\/WAI\/WCAG21\/quickref\/\" target=\"_blank\">WCAG Quick Reference<\/a> &#8211; A guide to understanding the guidelines.<\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Accessibility\" target=\"_blank\">MDN Web Docs on Accessibility<\/a> &#8211; Documentation on accessible front-end practices.<\/li>\n<li><a href=\"https:\/\/www.a11yproject.com\/\" target=\"_blank\">The A11Y Project<\/a> &#8211; A community-driven effort to help developers with accessibility.<\/li>\n<\/ul>\n<h2>Get Involved!<\/h2>\n<p>What accessible components have you built with Vanilla JavaScript? Share your thoughts and experiences in the comments below. Let\u2019s learn and collaborate to create a more inclusive web together!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building Accessible Components with Vanilla JavaScript Creating accessible web applications is not just a best practice; it\u2019s a necessity for reaching the entire audience. In this article, we will delve into building accessible components using Vanilla JavaScript. By adhering to the principles of accessibility, you make your applications usable for people with disabilities, ensuring a<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[202],"tags":[1283,335,1284],"class_list":["post-10471","post","type-post","status-publish","format-standard","category-ui-ux-design","tag-accessibility-tools","tag-best-practices","tag-keyboard-accessibility"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10471"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10471\/revisions"}],"predecessor-version":[{"id":10472,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10471\/revisions\/10472"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}