Skip to content

Instantly share code, notes, and snippets.

@rungta
Last active August 29, 2015 14:21
Show Gist options
  • Save rungta/b8acdfee905356a3431f to your computer and use it in GitHub Desktop.
Save rungta/b8acdfee905356a3431f to your computer and use it in GitHub Desktop.
A jQuery plugin to find a form field’s associated `label` elements.
/*
jQuery plugin to find all <label> elements
associated with the currently selected form field
elements (typically <input>, <select> or <textarea>)
eg: $('input').labels();
*/
$.fn.labels = function () {
return this.map(function () {
if (this.labels) {
return $(this.labels).toArray();
}
return $('label[for=' + this.id + ']')
.toArray()
.concat(
$(this)
.closest('label')
.not('[for]')
.toArray()
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment