-1

I'm working on WCAG compliance and I need to fix "Empty link" errors.

This is an example of one of the elements providing the error:

<a style="bottom: 0px; left: 7px; width: 25px; cursor: default;" class="histogram_bar_link" title="Less than 2,016 (0)" href="#"
    onclick="AjaxControlToolkit.SliderRefinementControl.FindAndSetSliderHandlesAndValuesToRange(" ctl00_ctl43_g_453d6d26_9535_440a_ba72_b7b88de22f31_csr5
    ", 0, 1, this); return false;" activebarid="ctl00_ctl43_g_453d6d26_9535_440a_ba72_b7b88de22f31_csr5_HistogramActiveBar0" disabled="disabled">
    <div id="ctl00_ctl43_g_453d6d26_9535_440a_ba72_b7b88de22f31_csr5_HistogramBgBar0" class="histogram_bar_background">
        <div id="ctl00_ctl43_g_453d6d26_9535_440a_ba72_b7b88de22f31_csr5_HistogramActiveBar0" class="histogram_bar_active" style="height: 0px; margin-top: 50px;">
    </div>
</a>

I need to find this element and other anchor tags which contain no text within so I can fix them through JS

Batman
  • 5,563
  • 18
  • 79
  • 155
  • 2
    How does `&nbsp` help a blind person more than `""`? I'm not sure you've thought this through. – user229044 May 04 '17 at 17:20
  • You're right, I was actually thinking more about removing the error than actually fixing the problem. But if I insert text there it completely destroys the slider, so what would be a recommended solution for something like this? – Batman May 04 '17 at 17:23
  • 2
    What you actually need to do is provide sensible text that is read aloud by a screen reader when that link gets focus, then visually hide that text from a sighted user. Filling those with emptiness may shut-up the compliance _checker_ but it doesn't actually help _compliance_. – Stephen P May 04 '17 at 17:23
  • Could I inject text and apply font 0 so the reader could read it and it wouldn't break the slider? – Batman May 04 '17 at 17:24

1 Answers1

2

This is not a link. Do not try to fix it client-side, just correct the HTML. Ideally if you update whatever control generates that HTML, then you do not need to rely on client-side script.

1. Choosing the right control

Let's address the first issue, using the right element...

Does the Control Take Me to Another Page? Use an Anchor

If, when clicked, tapped, or activated by keyboard or voice (or insert novel interaction method here), the user is whisked to another URL (including an anchor on the same page), then use <a href="[URL]">. Make sure you use the href attribute and that it has a real URL, not a “#” (otherwise you’re probably relying on JavaScript, which is not at all necessary for a hyperlink). If an href points to just a “#”, then you’re probably doing it wrong. If it points to a named anchor as part of your progressive enhancement efforts, then that’s totally valid.

Does the Control Change Something on the Current Page? Use a Button

If, when activated, the user is not moved from the page (or to an anchor within the page), but instead is presented with a new view (message boxes, changes in layout, etc.), then use a <button>. While you could use an <input type="button">, it’s more likely you’ll get into conflicts with pre-existing styles and subsequent developers (like me).

Does the Control Submit Form Fields? Use a Submit

If, when activated, information the user has entered (either by manually typing or by choosing items on the screen) is being sent back to the server, then use an <input type="submit">. This had better live within a <form>. If you need more styling control or have to embed more than just a simple text string, use a <button type="submit"> instead. I tend to prefer <input type="submit"> as I find it runs into fewer conflicts (both mentally and stylistically) with developers.

Keyboard User Consideration

Think of keyboard users for a moment. A hyperlink can be fired by pressing the enter key. But a true button can be fired by pressing the enter key or the space bar. When a hyperlink has focus and the user presses the space bar, the page will scroll one screenful. If there isn’t more to scroll then the user just experiences nothing. Given a set of interface elements that look the same, if some work with a space bar and some don’t, you can’t expect users to have much confidence in how the page behaves.

I have a CodePen demo that shows this in action: http://s.codepen.io/aardrian/debug/PZQJyd

I think it’s also worth mentioning that events triggered by a space bar only fire when the key is released, whereas using the Enter key will fire the event as soon as you press the key down (prior to releasing it).

For reference: http://adrianroselli.com/2016/01/links-buttons-submits-and-divs-oh-hell.html

2. Giving it an accessible name

Now let's talk about how you get an accessible name into that button. I assume you do not want it to be seen visually. In that case, give it a class of visually-hidden and apply these styles:

.visually-hidden {
  position: absolute !important;
  clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
  clip: rect(1px, 1px, 1px, 1px);
  padding:0 !important;
  border:0 !important;
  height: 1px !important;
  width: 1px !important;
  overflow: hidden;
 }

This will allow it to be read by screen readers. It also means you can (and should) dump that title attribute.

3. Combine it

Now take your control and adjust it accordingly:

<button style="…" class="histogram_bar_link" onclick="…" disabled="disabled">
  <span class="visually-hidden">Less than 2,016 (0)</span>
  <div id="…" class="histogram_bar_background">
    <div id="…" class="histogram_bar_active" style="…"></div>
  </div>
</button>

I have turned it into a <button> and given it an accessible name by inserting a <span> with the visually-hidden class to hide it from view.

Community
  • 1
  • 1
aardrian
  • 8,581
  • 30
  • 40