Last active
August 29, 2015 14:21
-
-
Save rungta/b8acdfee905356a3431f to your computer and use it in GitHub Desktop.
A jQuery plugin to find a form field’s associated `label` elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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