What fresh hell is THIS now? - Patrick Lauke
There is a rising popular technique where a <label>
is styled to mimic the appearance of a placeholder
attribute.
<label><span>Search the site</span><input type="search"></label>
An issue arises where “Search the site” is repeated twice, once for the <label>
or <span>
and then again for the <input>
. Using <span aria-hidden="true">
makes it so the label is not read at all, but then using <input type="search" aria-label="Search the site">
resolves this.
<label><span aria-hidden="true">Search the site</span><input type=“search” aria-label="Search the site"></label>
The tripled redundency in the code recreates the intended experience for the screen reader. Are there better ways to do this?
<label><span data-label>Search the site</span><input type="search"></label>
function defineAria(input) { var label = input.parentElement.querySelector('[data-label]'); if (label) { label.setAttribute('aria-hidden', true); input.setAttribute('aria-label', label.innerHTML.replace(/<[^\s][^>]*>/g, '')); } } Array.prototype.forEach.call(document.querySelectorAll('button,input,select,textarea'), defineAria);
div
for a span
and just <div class=“floaty-label-thingy”><label for=“dingy”>Dingy</label><input id=“dingy”></div>
?
<div>
wrapper as you recommended:
<div class="form-group"><label for="search-more">Search the site</label><input id="search-more" type="search"></div>
function setAriaAttributes(input) { if (input.id) { var label = document.querySelector('label[for="' + input.id + '"]'); if (label) { label.setAttribute('aria-hidden', true); input.setAttribute('aria-label', label.innerHTML.replace(/<[^\s][^>]*>/g, '')); } } } Array.prototype.forEach.call(document.querySelectorAll('button,input,select,textarea'), setAriaAttributes);
<input id=s type=search oninput="this.setAttribute('data-value', this.value);" data-value> <label for=s>Search</label> </div> /* CSS */ div { position: relative; } label { position: absolute; left: 0.5em; opacity: 0; } input[data-value=""] + label { opacity: 0.5; }
Control + Command
and VoiceOver’s is Control + Alt
.
aria-label
or aria-hidden
if you are using a real <label>
rel=“canonical"
or something like that.