Reputation: 13
Is there a magic jQuery selector that can select labels of input fields?
like $('#myinput').getLabel()
or something?
or do I really have to use $('label[for="myinput"]')
?
Upvotes: 1
Views: 2461
Reputation: 41620
You can always define it as a jQuery extension:
$.fn.getLabel = function () {
var name = this.attr('name');
if (!name) {
// No id, so we can't find the label
return $();
}
return $('label[for="' + name+ '"]');
};
(You might also want to handle possible "
s in the id somehow)
Upvotes: 4