Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created October 6, 2011 11:07
Show Gist options
  • Save sfoster/1267150 to your computer and use it in GitHub Desktop.
Save sfoster/1267150 to your computer and use it in GitHub Desktop.
Identify unlabelled inputs on the page (wcag2) (uses dojo)
(function(){
// usage:
// dojo.create("script", { src: 'https://raw.github.com/gist/1267150/b786273d696a119bba26c4590b1608aa9a21755f/gistfile1.txt?'+(new Date).getTime(), type: 'text/javascript' }, dojo.query("head", document.documentElement).shift())
var labelMap = {};
dojo.query("label[for]").forEach(function(lbl) {
labelMap[lbl.htmlFor]=lbl;
});
console.log("labelMap: ", labelMap);
var selector = [
"input[type=text]",
"input[type=password]",
"input[type=checkbox]",
"input[type=radio]",
"input[type=file]",
"select",
"textarea"
].join(',');
var badInputs = dojo.query(selector).filter(function(elm) {
console.log("id: %s, title: %s", elm.id, elm.title, elm);
if(elm.title) return false;
var id = elm.id;
console.log("labelled-by: ", labelMap[id]);
return !(id && labelMap[id]==elm);
});
console.log("unlabelled inputs:", badInputs);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment